vue2.5入门——3.vue-cli的使用

一、vue-cli的简介 与使用

1、安装vue-cli

npm install –global vue-cli
vue init webpack todolist
cd todolist
npm run dev

2、项目文件目录

\index.html整个项目的html文件。

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>todolist</title>
  </head>
  <body>
    <div id="app"></div>
    <!-- built files will be auto injected -->
  </body>
</html>

\src\main.js整个项目的入口文件

import Vue from 'vue'
import App from './App'

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  components: { App },
  template: '<App/>'
})

注册了一个局部组件。是从当前目录下APP引入的组件。

\src\app.vue

<template>
  <div id="app">
    <img src="./assets/logo.png">
    <HelloWorld/>
  </div>
</template>

<script>
import HelloWorld from './components/HelloWorld'

export default {
  name: 'App',
  components: {
    HelloWorld
  }
}
</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-cli开发TodoList

template里面只能有一个最外层的包裹元素。
之前data:{}作为对象使用。但是在脚手架工具里面,data数据应该变成一个函数。返回值是具体的数据。

\src\TodoList.vue

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

  <script>

  import TodoItem from './components/TodoItem'

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

  <style>
  </style>

\src\components\TodoItem.vue

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

<script>
    export default {
        props: ['content','index'],
        methods:{
            handleDelete () {
                this.$emit('delete_one',this.index)//向外出发一些事件
            }
        }
    }
</script>

<style></style>

三、全局样式与局部样式

<style scoped>
    .item{
        color: green
    }
</style>

scoped作用域限制,只对当前组件有影响。

四、课程总结

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值