<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>todolist</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head> <body>
<div id="app">
<input type="text" name="" id="" v-model="inputValue">
<button v-on:click="handleBtnClick">提交</button>
<!-- 把li标签里的内容整体变成一个组件 -->
<ul>
<!-- <li v-for="item in list">{{item}}</li> -->
<!-- v-bind:向子组件传递一个绑定值 -->
<todo-item v-bind:content="item" v-for="item in list">
</todo-item>
</ul>
</div> <script>
// Vue.component('TodoItem', {
// props: ['content'], //从父组件接收的内容
// template: '<li>{{content}}</li>',
// }); //vue提供的创建全局组件的方法 //注册局部对象
var TodoItem = {
props: ['content'], //定义接收外部传递过来的数据
template: '<li>{{content}}</li>', //子组件的模板
}; var app = new Vue({
el: '#app',
components: {
TodoItem: TodoItem, //把局部组件注册到实例中
},
data: {
list: [],
inputValue: ''
},
methods: {
handleBtnClick: function() {
this.list.push(this.inputValue);
this.inputValue = '';
}
}
});
</script>
</body> </html>
05-16 00:18