web前端开发之vue.js: 使用vue-cli脚手架实战小项目
【摘要】 实现一个脚手架(vue-cli)创建的一个简单 to do list 实例。简单实现输入框获取待办事项,并显示在下方list可以单击事项添加删除线 意味已办端口设置为6060环境我用的Windows系统node.js v12.18.3npm 6.14.6vue.js这些还没配置好的可以去classroom里的web前端第三阶段学习或者看我之前发布的博客内容效果项目目录src/A...
实现一个脚手架(vue-cli)创建的一个简单 to do list 实例。
简单实现
输入框获取待办事项,并显示在下方list
可以单击事项添加删除线 意味已办
端口设置为6060
环境
我用的Windows系统
node.js v12.18.3
npm 6.14.6
vue.js
这些还没配置好的可以去classroom里的web前端第三阶段学习或者看我之前发布的博客内容
效果

项目目录

src/App.vue 文件代码:
<template>
<div id="app">
<h1 v-html = "title"></h1>
<input v-model="newItem" v-on:keyup.enter="addNew" ></input>
<ul>
<li v-for="item in items" v-bind:class="{finished:item.isFinished}" v-on:click="toggleFinish(item)">{{item.label}}</li>
</ul>
</div>
</template><script>import Store from './store'export default {
data:function(){
return {
title:"This is a Todolist",
items:Store.fetch(),
newItem:""
}
},
watch:{
items:{
handler:function(items){
Store.save(items)
},
deep:true
}
},
methods:{
toggleFinish:function(item){
item.isFinished = !item.isFinished },
addNew:function(){
this.items.push({
label:this.newItem,
"isFinished":false
})
this.newItem=""
}
}}</script><style>.finished{
text-decoration:line-through;}li{
list-style:none;
font-size:1.6em;
margin-top:10px;}#app {
background-image:url(./576df1167c887_1024.jpg);
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;}input{
width:230px;
height:40px;
border-radius:20px;
padding: 0.4em 0.35em;
border:3px solid #CFCFCF;
font-size: 1.55em;}</style>
src/store.js 实现简单存储数据效果,即刷新数据不消失,保留list:
const STORAGE_KEY='todos-vuejs'export default {
fetch:function(){
return JSON.parse(window.localStorage.getItem(STORAGE_KEY)||'[]');
},
save:function(items){
window.localStorage.setItem(STORAGE_KEY,JSON.stringify(items))
}}
操作流程
1.控制台进入项目目录 npm install 导入node_modules依赖包
2.npm run dev运行项目
注意:npm命令需要提前安装node.js

浏览器打开网址:http://localhost:6060/ 即可出现效果
文末附源码
书山有路勤为径,学海无涯苦作舟。
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)