..._vue中一个组件相当于一个实例吗">
赞
踩
组件与实例的关系
代码如下
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Vue学习</title>
- <script src="./vue.js"></script>
- </head>
- <body>
- <div id="root">
- <div>
- <input v-model="inputValue"/>
- <button @click="handleClick">提交</button>
- </div>
- <ul>
- <todo-item v-for="(item,index) of list"
- :key="index"
- :content="item"
- >
- </todo-item>
- </ul>
- </div>
- <script>
- Vue.component('todo-item',{
- props: ['content'],
- template: '<li @click="handleClick">{{content}}<li>',
- methods: {
- handleClick: function () {
- alert('click')
-
- }
-
- }
- })
- new Vue({
- el: "#root",
- data:{
- inputValue: '',
- list: []
- },
- methods: {
- handleClick: function () {
- this.list.push(this.inputValue)
- this.inputValue = ''
-
- }
- }
-
- })
- </script>
-
- </body>
- </html>
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
页面展示效果
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。