Vue组件使用–示例一
<!DOCTYPE html>
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> Title</ title>
< script src = " https://cdn.staticfile.org/vue/2.2.2/vue.min.js" > </ script>
</ head>
< body>
< div id = " app" >
< ol>
< li> ddsss</ li>
< todo-item> </ todo-item>
< todo-item> </ todo-item>
< todo-item> </ todo-item>
</ ol>
</ div>
< script>
Vue. component ( 'todo-item' , {
template: "<li>This is a todo</li>"
} ) ;
var app= new Vue ( {
el: "#app" ,
} ) ;
</ script>
</ body>
</ html>
Vue组件示例二:从父作用域将数据传到子组件
<!DOCTYPE html>
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> Title</ title>
< script src = " https://cdn.staticfile.org/vue/2.2.2/vue.min.js" > </ script>
</ head>
< body>
< div id = " app" >
< ol>
< todo-item v-for = " item in groceryList"
v-bind: todo= " item"
v-bind: key= " item.id" > </ todo-item>
</ ol>
</ div>
< script>
Vue. component ( 'todo-item' , {
props: [ 'todo' ] ,
template: "<li>{{todo.text}}</li>"
} ) ;
var app= new Vue ( {
el: "#app" ,
data: {
groceryList: [
{ id: 0 , text: '蔬菜' } ,
{ id: 1 , text: '奶酪' } ,
{ id: 2 , text: '白菜' } ,
]
}
} ) ;
</ script>
</ body>
</ html>
Vue示例三 列表渲染-简单的 todo list 的完整例子
示例参照连接
<!DOCTYPE html>
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> Title</ title>
< script src = " https://cdn.staticfile.org/vue/2.2.2/vue.min.js" > </ script>
</ head>
< body>
< div id = " app" >
< form v-on: submit.prevent= " addNams" >
< label> 姓名</ label> < input v-model = " newName" placeholder = " 请输入姓名" >
< button> 添加</ button>
</ form>
< ul>
< li
is = " name-item"
v-for = " (item,index) in peoples"
v-bind: key= " item.id"
v-bind: name= " item.name"
v-on: remove22= " peoples.splice(index,1)" > </ li>
</ ul>
</ div>
< script>
Vue. component ( 'name-item' , {
template: '<li>{{name}}' +
'<button v-on:click="$emit(\'remove22\')">remove</button></li>' ,
props: [ 'name' ]
} ) ;
new Vue ( {
el: '#app' ,
data ( ) {
return {
newName: '' ,
peoples: [ { id: 1 , name: '小明' } , { id: 2 , name: '小明2' } , { id: 3 , name: '小明3' } ] ,
newId: 4
}
} ,
methods: {
addNams ( ) {
if ( this . newName!= '' ) {
this . peoples. push ( { id: this . newId, name: this . newName} ) ;
this . newId++ ;
this . newName= "" ;
}
}
}
} ) ;
</ script>
</ body>
</ html>