我是靠谱客的博主 优雅小土豆,这篇文章主要介绍Vue小例子-todolist功能开发Vue-cli的简介与使用,现在分享给大家,希望可以做个参考。

1、最简单的todolist

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="./vue.js"></script> </head> <body> <div id="root"> <div> <input v-model="inputValue"> <button @click="handelSubmit">提交</button> </div> <ul> <li v-for="(item,index) of list" :key="index">{{item}}</li> </ul> </div> <script> new Vue({ el: "#root", data: { inputValue: '', list: [] }, methods: { handelSubmit: function () { this.list.push(this.inputValue) this.inputValue = '' } } }) </script> </body> </html>

2、todolist组件拆分

①:局部组件

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="./vue.js"></script> </head> <body> <div id="root"> <div> <input v-model="inputValue"> <button @click="handelSubmit">提交</button> </div> <ul> <todo-item></todo-item> </ul> </div> <script> //全局组件 // Vue.component( // 'todo-item', { // template: '<li>item</li>' // } // ) //局部组件 var TodoItem = { template: '<li>item</li>' } new Vue({ el: "#root", components: { 'todo-item': TodoItem }, data: { inputValue: '', list: [] }, methods: { handelSubmit: function () { this.list.push(this.inputValue) this.inputValue = '' } } }) </script> </body> </html>

②:全局组件拆分

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="./vue.js"></script> </head> <body> <div id="root"> <div> <input v-model="inputValue"> <button @click="handelSubmit">提交</button> </div> <ul> <todo-item v-for="(item,index) of list" :key="index" :content="item"></todo-item> </ul> </div> <script> //全局组件 //外部组件对内部组件(todo-item)传值,内部组件通过props接受传值 Vue.component( 'todo-item', { props: ['content'], template: '<li>{{content}}</li>' } ) new Vue({ el: "#root", data: { inputValue: '', list: [] }, methods: { handelSubmit: function () { this.list.push(this.inputValue) this.inputValue = '' } } }) </script> </body> </html>

3、组件与实例的关系

①:每个组件都是vue的一个实例(任何一个vue项目都是由千千万万实例组成的)

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="./vue.js"></script> </head> <body> <div id="root"> <div> <input v-model="inputValue"> <button @click="handelSubmit">提交</button> </div> <ul> <todo-item v-for="(item,index) of list" :key="index" :content="item"></todo-item> </ul> </div> <script> //全局组件 //外部组件对内部组件(todo-item)传值,内部组件通过props接受传值 Vue.component( 'todo-item', { props: ['content'], template: '<li @click="handleClick">{{content}}</li>', methods: { handleClick: function () { alert('clicked') } } } ) new Vue({ el: "#root", data: { inputValue: '', list: [] }, methods: { handelSubmit: function () { this.list.push(this.inputValue) this.inputValue = '' }, } }) </script> </body> </html>

4、todolist的删除功能

父组件向子组件传递值(通过属性传值)

子组件向父组件传值(子组件向父组件发送emit模式,父组件监听这个子组件)

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="./vue.js"></script> </head> <body> <div id="root"> <div> <input v-model="inputValue"> <button @click="handelSubmit">提交</button> </div> <ul> <todo-item v-for="(item,index) of list" :key="index" :content="item" :index="index" @delete="handleDelte"> </todo-item> </ul> </div> <script> //全局组件 //外部组件对内部组件(todo-item)传值,内部组件通过props接受传值 Vue.component( 'todo-item', { props: ['content', 'index'], template: '<li @click="handleClick">{{content}}</li>', methods: { handleClick: function () { this.$emit('delete', this.index) } } } ) new Vue({ el: "#root", data: { inputValue: '', list: [] }, methods: { handelSubmit: function () { this.list.push(this.inputValue) this.inputValue = '' }, handleDelte: function (index) { this.list.splice(index, 1) } } }) </script> </body> </html>

Vue-cli的简介与使用

①npm install -g cnpm --registry=https://registry.npm.taobao.org

②vue init webpack todolist

③ cd todolist

④cnpm instal

 

全局样式和局部央视:scoped控制局部作用域

最后

以上就是优雅小土豆最近收集整理的关于Vue小例子-todolist功能开发Vue-cli的简介与使用的全部内容,更多相关Vue小例子-todolist功能开发Vue-cli内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(79)

评论列表共有 0 条评论

立即
投稿
返回
顶部