美文网首页
VUE组件(传值,生命周期)

VUE组件(传值,生命周期)

作者: 卐_c2a9 | 来源:发表于2018-09-24 11:38 被阅读0次

VUE生命周期

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
   <div id='app'>{{msg}}</div>
    <script src='js/vue.js'></script>
    <script>
       new Vue({
           el:'#app',
           data:{
               msg:'hello vue'
           },
           beforeCreate:function(){
               alert('beforeCreated');
           },
           created:function(){
               alert('created')
           },
           beforeMount:function(){
                 alert('beforMount')
           },
           mounted:function(){
               alert('mounted')
           }
       })
    </script>
</body>
</html>

VUE子传父组件通信

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
   <div id='app'>
       <chat></chat>
   </div>
    <script src='js/vue.js'></script>
    <script>
    Vue.component('chat',{
        template:`
           <div>
              <ul>
                 <li v-for="value in arr">{{value}}</li>
              </ul> 
              <user @send='rcvMsg' userName='jack'></user>
              <user @send='rcvMsg' userName='rose'></user>
           </div>
       `,
        data:function(){
            return{
                arr:[]
            }
        },
        methods:{
            rcvMsg:function(txt){
                this.arr.push(txt)
            }
        }
    })  
    
    Vue.component('user',{
        props:['userName'],
        template:`
          <div>
             <label>{{userName}}</label>
             <input type='text' v-model='inputVal'>
             <button @click='sendMsg'>发送</button>
          </div>
        `,
        data:function(){
            return{
                inputVal:''
            }
        },
        methods:{
          sendMsg:function(){
              this.$emit('send',this.userName+':'+this.inputVal)
          }
        }
    }) 
    new Vue({
        el:'#app'
    })
    </script>
</body>
</html>

VUE非父子组件传值

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
   <div id='app'>
       <child></child>
       <son></son>
   </div>
    <script src='js/vue.js'></script>
    <script>
       var bus=new Vue();
       Vue.component('child',{//A
           template:`
            <div>
               <h1>我是child组件</h1>
               <button @click='sendMsg'>发送数据给son</button>
            </div>
           `,
           data:function(){
               return{
                   msg:'hello vue'
               }
           },
           methods:{
               sendMsg:function(){
                  bus.$emit('send',this.msg) 
               }
           }
       }) 
        
       Vue.component('son',{//B
           template:`
            <div>
               <h1>我是son组件</h1>
               <a href=''>{{mess}}</a>
            </div>
           `,
           data:function(){
               return{
                   mess:''
               }
           },
           mounted:function(){
               bus.$on('send',msg=>{//箭头函数
                   console.log(this);
                   this.mess=msg
               })
           }
           
       }) 
        
       new Vue({
           el:'#app'
       })
       
//       v-HTML
//       v-text
//       v-cloak
    </script>
</body>
</html>

相关文章

  • VUE组件(传值,生命周期)

    VUE生命周期 VUE子传父组件通信 VUE非父子组件传值

  • 与Vue.js的第八天

    今天学习了Vue组件中的非父子之间的传值和生命周期Vue组件之间的传值分三种1.父传子之间传值用属性:props2...

  • Vue - 传值

    Vue 传值有两种:1)父组件向子组件传值(属性传值,Prop传值)2)子组件向父组件传值 一、父组件向子组件传值...

  • (VUE3) 四、组件传值(父子组件传值 & 祖孙组件传值 &v

    1.父子组件传值 vue2中的父子组件传值:父组件: 子组件: vue3中的父子组件传值: 还是用props接收父...

  • 前端基础搬运工-VUE模块

    十、VUE模块 基础部分 1. Vue组件间传值 答: -[ ] 1.父子之间的传值 父组件向子组件传值通过p...

  • Composition API的使用

    目标 父子组件传值props 和 context 祖孙组件传值provice和inject 生命周期 on**...

  • Vue父子组件通信和双向绑定

    本篇文章主要介绍父子组件传值,组件的数据双向绑定。 1. 基础父子组件传值 父子组件传值,这是Vue组件传值最常见...

  • 2019-03-13

    vue父子组件传值,(父组件向子组件传值用prop ,子组件向父组件传值:子组件调用父组件方法值以参数的方式传递)...

  • 2019-03-13

    vue父子组件传值,(父组件向子组件传值用prop ,子组件向父组件传值:子组件调用父组件方法值以参数的方式传递)...

  • vue2.0的三种常用传值方式,并且如何实现?

    vue2.0 组件传值方式有三种:父组件向子组件传值,子组件向父组件传值,非父子组件传值 : 父传子: 首先现在父...

网友评论

      本文标题:VUE组件(传值,生命周期)

      本文链接:https://www.haomeiwen.com/subject/ixbcoftx.html