vue 脚手架 firstDemo

七亿少女的劫丨

学习Vue js 2.0 第一天-2

在这里插入图片描述
使用vue-cli( 脚手架) 创建vue项目
1.学会定义vue组件
2.标签化组件

目录结构:
在这里插入图片描述
主要关注src目录
在这里插入图片描述
Todos.vue 代码:

<template>
  <ul class="list-group">
    <li class="list-group-item"
        v-bind:class="{'completed':todo.completed}"
        v-for="(todo,index)  in todos">{{todo.title}}
      <button type="button" class="btn btn-warning btn-xs  pull-right"
              v-on:click="deletetodo(index)">deleted
      </button>
      <button type="button" class="btn  btn-xs  pull-right margin-right-10"
              v-bind:class="[todo.completed ? 'btn-danger':'btn-success']"
              v-on:click="toggleCompletion(todo)">{{todo.completed ? 'undo' :'completed'}}
      </button>
    </li>
  </ul>
</template>
<script>
  export default {
    props:['todos'],
    methods:{
      deletetodo(index){
        this.todos.splice(index,1)
      },
      toggleCompletion(todo){
        todo.completed = ! todo.completed
      }
    }
  }
</script>
<style scoped>
  .completed {
    color: #4cae4c;
    text-decoration:line-through;
  }
  .margin-right-10{
    margin-right: 10px;
  }
</style>

TodoForm.vue代码:

<template>
  <form v-on:submit.prevent="addtodo(newtodo)">
    <div class="form-group" >
      <input type="text" v-model="newtodo.title" class="form-control" placeholder="Add a new">
    </div>
    <div class="form-group">
      <button class="btn btn-success" type="submit">Add Todo</button>
    </div>
  </form>
</template>
<script>
  export default {
    props:['todos'],
    data(){
      return{
        newtodo:{id:null,title:'',completed:false}
      }
    },
    methods:{
      addtodo(newtodo) {
        this.todos.push(newtodo);
        this.newtodo={id:null,title:'',completed:false}
      }
    }
  }
</script>
<style scoped>
</style>

在app.vue中声明 使用Todo.vue和TodoForm.vue

<template>
  <div id="app">
    <!--<img src="./assets/logo.png">-->
    <!--<router-view/>-->
    <todos :todos="todos"></todos>
    <todo-form :todos="todos"></todo-form>
  </div>
</template>
<script>
  import TodoForm from "@/components/TodoForm"
  import Todos from "@/components/Todos";
  export default {
    name: 'App',
    data(){
      return{
        todos:[
          {id:1,title:'learn Vue js',completed:false}
        ]
      }
    },
    computed:{
      todoCount(){
        return this.todos.length
      }
    },
    components: {TodoForm, Todos},
  }
</script>
<style>
  #app {
    font-family: 'Avenir', Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-align: center;
    color: #2c3e50;
    margin-top: 60px;
  }
</style>

vue最好使用驼峰命名
注意Todos.vue,TodoForm.vue使用时,写成标签写为:

  <todos :todos="todos"></todos>
  <todo-form :todos="todos"></todo-form>

运行结果:
在这里插入图片描述
效果和使用CDN 引入vue js 2.0 效果一样
但是 文件,组件更加分散,便于管理,逼格更高!!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值