<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<script src="vue.js"></script>
</head>
<body>
<div>
<h1>--访问子组件或子元素--</h1>
<div id="example1">
<base-input></base-input>
</div>
<script>
// 定义组件
Vue.component('base-input', {
methods: {
add: function () {
this.$refs.usernameInput.value = "Hello";
}
},
template: '\
<div>\
<input type="text" value="Hi" ref="usernameInput"></input>\
<button v-on:click="add">添加</button>\
</div>\
'
})
// Vue 根实例
var example1 = new Vue({
el: '#example1',
})
</script>
</div>
</body>
</html>