Vue入门(十)

Vue-cli的使用——使用vue-cli开发TodoList

首先准备好上一讲创建的项目 

1、用编辑器打开 todolist 项目后找到 scr 文件夹下的 App.vue 文件,重命名为 TodoList.vue 文件。

2、然后找到 main.js 文件,修改图中的代码为:

import Vue from 'vue'
import TodoList from './TodoList.vue'

Vue.config.productionTip = false

new Vue({
	render: h => h(TodoList),
}).$mount('#app')

现在正式开始编写代码

打开  TodoList.vue 文件修改代码为:

<template>
	<div>
		<input />
		<button>提交</button>
	</div>
	<ul>
		<li>1</li>
		<li>2</li>
		<li>3</li>
	</ul>
</template>

<script>

</script>

<style>

</style>

将代码保存后返回界面发现报错

报错的原因是因为 Vue 规定 <template></template>(模板标签)中只能有一个根元素,刚刚写的代码中 <div> 和 <ul> 是同一级元素,导致模板标签中有两个根元素,所以报错。只要把代码都包含到一个根元素中就行了,如下:

<template>
	<div>
		<div>
			<input />
			<button>提交</button>
		</div>
		<ul>
			<li>1</li>
			<li>2</li>
			<li>3</li>
		</ul>
	</div>
</template>

效果:

下面开始编写逻辑代码

        代码和之前的代码大同小异。

TodoList.vue 文件

<template>
	<div>
		<div>
			<input v-model="inputValue" />
			<button @click="handleSubmit">提交</button>
		</div>
		<ul>

		</ul>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				inputValue: '',
				list: []
			}
		},
		methods: {
			handleSubmit() {
				this.list.push(this.inputValue)
				this.inputValue = ''
			}
		},
	}
</script>

<style>

</style>

Vue-cli组件写在单独 vue 文件里

1、将 components 文件夹下的 HelloWorld.vue 文件,重命名为 TodoItem.vue 文件。

2、在 TodoItem.vue 文件中编写组件

<template>
	<li>item</li>
</template>

<script>
	export default {

	}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>

效果:

3、在 TodoList.vue 文件中引入 TodoItem 组件

<template>
	<div>
		<div>
			<input v-model="inputValue" />
			<button @click="handleSubmit">提交</button>
		</div>
		<ul>
			<todo-item></todo-item>
		</ul>
	</div>
</template>

<script>
	import TodoItem from './components/TodoItem.vue' // 引入组件
	export default {
		components: { // 注册组件
			'todo-item': TodoItem
		},
		data() {
			return {
				inputValue: '',
				list: []
			}
		},
		methods: {
			handleSubmit() {
				this.list.push(this.inputValue)
				this.inputValue = ''
			}
		},
	}
</script>

<style>

</style>

4、编写逻辑代码

<template>
	<div>
		<div>
			<input v-model="inputValue" />
			<button @click="handleSubmit">提交</button>
		</div>
		<ul>
			<todo-item v-for="(item,index) in list"></todo-item>
		</ul>
	</div>
</template>

保存后发现会报错,提示必须要有 v-bind:key

修改为:<todo-item v-for="(item,index) in list" :key="index"></todo-item>

效果:

子组件接收父组件的传值

        父组件通过属性的形式(:content="item")向子组件传值

TodoList.vue 文件

<template>
	<div>
		<div>
			<input v-model="inputValue" />
			<button @click="handleSubmit">提交</button>
		</div>
		<ul>
			<todo-item v-for="(item,index) in list" :key="index" :content="item">
			</todo-item>
		</ul>
	</div>
</template>

TodoItem.vue 文件

<template>
	<li>{{content}}</li>
</template>

<script>
	export default {
		props: ['content'],
	}
</script>

效果:

再实现删除功能

TodoList.vue 文件

<template>
	<div>
		<div>
			<input v-model="inputValue" />
			<button @click="handleSubmit">提交</button>
		</div>
		<ul>
			<todo-item v-for="(item,index) in list" :key="index" :content="item" :index="index" @delete="handleDelete">
			</todo-item>
		</ul>
	</div>
</template>

<script>
	import TodoItem from './components/TodoItem.vue' // 引入组件

	export default {
		components: { // 注册组件
			'todo-item': TodoItem
		},
		data() {
			return {
				inputValue: '',
				list: []
			}
		},
		methods: {
			handleSubmit() {
				this.list.push(this.inputValue)
				this.inputValue = ''
			},
			handleDelete: function(index) {
				this.list.splice(index, 1)
			}
		},
	}
</script>

<style>

</style>

TodoItem.vue 文件

<template>
	<li @click="handleDelete">{{content}}</li>
</template>

<script>
	export default {
		props: ['content', 'index'],
		methods: {
			handleDelete() {
				// alert(this.index)
				this.$emit('delete', this.index)
			}
		}
	}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>

全局样式与局部样式

把每个列表项加一个颜色

TodoItem.vue 文件

<template>
	<li class="item" @click="handleDelete">{{content}}</li>
</template>

<script>
	export default {
		props: ['content', 'index'],
		methods: {
			handleDelete() {
				// alert(this.index)
				this.$emit('delete', this.index)
			}
		}
	}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
	.item {
		color: red;
	}
</style>

效果:

 

当我们在文本框加上样式

<template>
	<div>
		<div>
			<input class="item" v-model="inputValue" />
			<button @click="handleSubmit">提交</button>
		</div>
		<ul>
			<todo-item v-for="(item,index) in list" :key="index" :content="item" :index="index" @delete="handleDelete">
			</todo-item>
		</ul>
	</div>
</template>

效果:

 

发现并没有效果,这是因为 scoped 关键字。

项目创建时自带的注释解释了它的作用:

<!-- Add "scoped" attribute to limit CSS to this component only -->

翻译:添加“scoped”属性以将CSS仅限于此组件

所以添加了该关键字将样式变成了局部样式。如果去掉就变成全局样式如下:

 


视频学习地址: 课程介绍,vue2.5入门 教程-慕课网 (imooc.com)  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值