vue

2018-03-15  本文已影响0人  Love小六六

vue三大特点

vue实例对象

vue生命周期

vue组件

Vue.component('my-header',{
    template:'<div></div>',
    data:{
    }
})
var myHeader = {
    template:'<div></div>',
    data:{
    }
}
new Vue({
    el:'#app',
    data:{},
    compentnes:{
        'my-header':myHeader
    }
})
// 错误
data:{
    a:1
}
// 正确
data:function(){
    return{
        a:1
    }
}

vue基础

Vue父子组件

Vue动画

Vue自定义指令

v-xxx="value"; 
directives:{
    xxx:function(el,binding){
        //el为当前绑定指令的元素
        //binding为接受的value
    }
}

vue-router

import VRouter from 'vue-router'
Vue.use(VRouter)
let router = new VRouter({
    routes:[
        {
            path:'/apple',
            component:Apple
        },
        {
            path:'/banana',
            component:Banana
        },
    ]
})
new Vue({
    el:'#app',
    router,
    template:'<App/>'
    components:{App}
})
//在模板里设置vue-router
<router-view></router-view>
<router-link :to="{path:'apple'}">to Apple</router-link>
let router = new VRouter({
    routes:[
        {
            path:'/apple/:color',
            component:Apple
        },
        {
            path:'/banana',
            component:Banana
        },
    ]
})
// 通过this.$route.params来获取路由参数
let router = new VRouter({
    routes:[
        {
            path:'/apple',
            component:Apple,
            children:[
                {
                    path:'red',
                    component:RedApple
                },
                {
                    path:'green',
                    component:GreenApple
                },
            ]
        },
        {
            path:'/banana',
            component:Banana
        },
    ]
})
<router-link :to="{path:'apple/red'}">to Apple</router-link>
let router = new VRouter({
    routes:[
        {
            path:'/apple',
            component:Apple,
            name:'applePage'
        },
        {
            path:'/banana',
            component:Banana
        },
    ]
})
<router-link :to="{name:'applePage'}">to Apple</router-link>
let router = new VRouter({
    routes:[
        {
            path:'/',
            redirect:'/apple'
        },
        {
            path:'/apple',
            component:Apple,
            name:'applePage'
        },
        {
            path:'/banana',
            component:Banana
        },
    ]
})
<transition name="fade">
    <keep-alive>
        <router-view></router-view>
    </keep-alive>
</transition>

vuex状态管理

// npm install vuex
import Vuex from 'vuex'
Vue.use(Vuex);
let store = new Vuex.store({
    state:{
        totalPrice:0
    },
    mutations:{
        increment(state,price){
            state.totalPrice += price;
        },
        decrement(state,price){
            state.totalPrice -= price;
        }
    },
    action:{
        increase(){context,price}{
            context.commit('increment',price);
        }
    }
})
new Vue({
    el:#app,
    store,
    methods:{
        addOn(price){
            this.$store.dispatch('increase',price)
        }
    }
})

父子组件间传值

// App.vue
// 父组件要引用子组件
// 父组件模板可以直接向子组件传值
<template>
  <div id="app">
    <Son :tell_son="sendMsgToSon"></Son>
  </div>
</template>

<script>
import Son from './components/Son.vue';
export default {
  name: 'App',
  data(){
    return{
      sendMsgToSon:'Msg From Dad'
    }
  },
  components: {
    Son
  }
}
</script>

// Son.vue
// 子组件通过props获取父组件传的值
<template>
    <div>
        <h1>{{ tell_son }}</h1>
    </div>
</template>

<script>
    export default {
        name: 'Son',
        data () {
            return {
            }
        },
        props:['tell_son']
    }
</script>
// Child.vue
// 子组件通过$emit来向父组件传值
<template>
    <div>
        <button @click="sendMsgToFather">点击发送信息给父组件</button>
    </div>
</template>

<script>
    export default {
        name: 'Child',
        data () {
            return {
            }
        },
        methods:{
            sendMsgToFather(){
                this.$emit('tell_dad','Msg From Child');
            }
        }
    }
</script>

// 父组件通过on来获取信息
<template>
  <div id="app">
    <p>来自子组件的信息{{receiveMsgFromSon}}</p>
    <Child v-on:tell_dad="receiveMsg"></Child>
  </div>
</template>

<script>
import Child from './components/Child.vue'

export default {
  name: 'App',
  data(){
    return{
      receiveMsgFromSon:''
    }
  },
  components: {
    Child
  },
  methods:{
    receiveMsg(data){
      this.receiveMsgFromSon = data;
    }
  }
}
</script>
上一篇下一篇

猜你喜欢

热点阅读