2018-09-19 Vue 第八天

1.路由

路由:vue-router
是Vue的工具库 vue-router.js
下载:
npm install vue 下载vue
npm install vue-router 下载vue-router

        通过不同的url访问不同的页面
               spa(single page application)  单页面应用

···
<div id="star">

<router-link to='/home'>首页</router-link>
<router-link to='/detail'>详情页</router-link>

<router-view></router-view>
</div>
<script src="js/vue.js"></script>
<script src="js/vue-router.js"></script>
<script>
//2.创建组件
var Home={
template:<h1>这是首页</h1>
}

    var Detail={
        template:`
            <h1>这是详情页</h1>
        `
    }
    
    //3.配置路由
    var routes=[
        {path:'/home',component:Home},
        {path:'/detail',component:Detail}
    ]
    
    //4.创建一个路由实例
    const router=new VueRouter({
        routes:routes
    })
    
    //5.把路由挂在到vue实例上
    new Vue({
        el:'#star',
        router:router
    })
</script>

···

2. 生命周期

···
<div id='app'>{{msg}}</div>
<script src='js/vue.js'></script>
<script>
new Vue({
el:'#app',
data:{
msg:'hello vue'
},
beforeCreate:function(){
alert('beforeCreate')
},
created:function(){
alert('Created')
},
beforeMount:function(){
alert('befroeMounted')
},
mounted:function(){
alert('mounted')
}
})
</script>
···

3.非父子组件之间的通信

<div id="app">
<child></child>
<son></son>

</div>
<script src="js/vue.js"></script>
<script>
    //同级别相传  child 发送   son  接收
    var bus=new Vue();
    Vue.component('child',{
        template:`
           <div>
             <h2>我是child组件</h2>
             <button @click='sendMsg'>发送数据</button>
           </div>  
        `,
        data:function(){
            return{
                msg:'我是child组件中的数据,要传给son组件'
            }
        },
        methods:{
            sendMsg:function(){ //发送数据
               bus.$emit('send',this.msg) 
            }
        }
    })
    
    
    //设置 函数 用来接收
    
    Vue.component('son',{
        template:`
           <div>
            <h2>我是son组件</h2>
            <a>{{mess}}</a>
          </div>   
       `,
        data:function(){
            return{
                mess:''
            }
        },
        mounted:function(){
            bus.$on('send',msg=>{
                console.log(this);    
                    this.mess=msg
                    
                    })
        }
    })
    
    
    new Vue({
        el:'#app'
    })
</script>

···

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • vue笔记 一.vue实例 vue的生命周期 beforeCreate(创建前), created(创建后), b...
    秋殇1002阅读 1,090评论 0 1
  • 基本格式 以json的形式、将数据(支持所有格式)挂载在vue的data上、方法挂载在vue的methods上。 ...
    kirito_song阅读 821评论 0 21
  • # 传智播客vue 学习## 1. 什么是 Vue.js* Vue 开发手机 APP 需要借助于 Weex* Vu...
    再见天才阅读 3,697评论 0 6
  • vue 2.0 渐进式框架 MVC 单向通信 > m:model 数据层 保存数据 > v:view视图层 用户界...
    web前端ling阅读 784评论 0 0
  • 一:什么是闭包?闭包的用处? (1)闭包就是能够读取其他函数内部变量的函数。在本质上,闭包就 是将函数内部和函数外...
    xuguibin阅读 9,807评论 1 52