欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue組件父與子通信詳解(一)

 更新時間:2017年11月07日 11:30:21   作者:匿名的girl  
這篇文章主要為大家詳細介紹了vue組件父與子通信詳解,實現(xiàn)登錄窗口,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了vue組件父與子通信的具體代碼,供大家參考,具體內(nèi)容如下

一、組件間通信(父組件    -->  子組件)

步驟:

①父組件在調(diào)用子組件 傳值

<child-component myValue="123"> </child-component>

②在子組件中 獲取父組件傳來的值

Vue.component('child-component',{
  props:['myValue'],
  template:''
})

代碼1:

<!doctype html>
<html>
 <head>
 <meta charset="UTF-8">
 <title>父傳子</title>
 <script src="js/vue.js"></script>
 </head>
 <body>
 <div id="container">
 <p>{{msg}}</p>
 <parent-component></parent-component>
 </div>
 <script>
 // 在vue中一切都是組件
 //父傳子
 Vue.component("parent-component",{
  data:function(){
  return {
   gift:"傳家寶"
  }
  },
  template:`
  <div>
   <h1>這是父組件</h1>
   <hr>
   <child-component v-bind:myValue="gift"></child-component>
  </div>
  `
 })
 Vue.component("child-component",{
  props:["myValue"],
  template:`
  <div>
   <h1>這是子組件</h1>
   <p>{{"父傳遞的值:"+myValue}}</p>
  </div>
  `
 })
 new Vue({
  el:"#container",
  data:{
  msg:"Hello VueJs"
  }
 })
 </script>
 </body>
</html>

myValue是屬性名,必須都一樣……拿data中的用v-bind:或者:
props是property屬性,是個數(shù)組

代碼2:

<!doctype html>
<html>
 <head>
 <meta charset="UTF-8">
 <title>父子之間通信練習</title>
 <script src="js/vue.js"></script>
 </head>
 <body>
 <div id="container">
 <p>{{msg}}</p>
 <my-login></my-login>
 </div>
 <script>
/*
 登錄窗口
 創(chuàng)建4個組件,分別是my-label my-input my-button my-login(復合組件)
*/
 Vue.component("my-label",{
  props:["myLabel"],
  template:`
  <div>
   <label>{{myLabel}}</label>
  </div>
  `
 })
 Vue.component("my-input",{
  template:`
  <div>
   <input type="text"/>
  </div>
  `
 })
 Vue.component("my-button",{
  props:["myButton"],
  template:`
  <div>
   <button>{{myButton}}</button>
  </div>
  `
 })
 //復合組件
 Vue.component("my-login",{
  data:function(){
  return {
   uname:"用戶名",
   upwd:"密碼",
   login:"登錄",
   register:"注冊"
  }
  },
  template:`
  <div>
  <my-label v-bind:myLabel="uname"></my-label>
  <my-input></my-input>
  <my-label v-bind:myLabel="upwd"></my-label>
  <my-input></my-input>
  <my-button v-bind:myButton="login"></my-button> 
  <my-button v-bind:myButton="register"></my-button>
  </div>
  `
 })
 new Vue({
  el:"#container",
  data:{
  msg:"Hello VueJs"
  }
 })
 </script>
 </body>
</html>

代碼3:

<!DOCTYPE html>
<html>
<head lang="en">
 <meta charset="UTF-8">
 <script src="js/vue.js"></script>
 <title></title>
</head>
<body>

<div id="container">
 <my-login></my-login>
</div>

<script>

 Vue.component('my-label',{
 props:['labelName'],
 template:'<label>{{labelName}}</label>'
 })
 Vue.component('my-input',{
 props:['tips'],
 template:'<input type="text" :placeholder="tips"/>'
 })
 Vue.component('my-button',{
 props:['btnName'],
 template:'<button>{{btnName}}</button>'
 })

 Vue.component('my-login',{
 template:`
 <form>
  <my-label labelName="用戶名"></my-label>
  <my-input tips="請輸入用戶名"></my-input>
  <br/>
  <my-label labelName="密碼"></my-label>
  <my-input tips="請輸入密碼"></my-input>
  <br/>
  <my-button btnName="登錄"></my-button>
  <my-button btnName="注冊"></my-button>
 </form>
 `
 })


 new Vue({
 el: '#container',
 data: {
  msg: 'Hello Vue'
 }
 })
</script>

</body>
</html>

要拿到data中的數(shù)據(jù)就要v-bind,否則就不用。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 快速解決vue-cli在ie9+中無效的問題

    快速解決vue-cli在ie9+中無效的問題

    今天小編就為大家分享一篇快速解決vue-cli在ie9+中無效的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • element的el-tree多選樹(復選框)父子節(jié)點關聯(lián)不關聯(lián)

    element的el-tree多選樹(復選框)父子節(jié)點關聯(lián)不關聯(lián)

    最近想要實現(xiàn)多選框關聯(lián)的功能,但是卻出現(xiàn)了element的el-tree多選樹(復選框)父子節(jié)點關聯(lián)不關聯(lián)的問題,本文就來介紹一下解決方法,一起來了解一下
    2021-05-05
  • Vue3中如何使用SCSS編寫樣式

    Vue3中如何使用SCSS編寫樣式

    在Vue模板中啟用這些表現(xiàn)力庫插件的最簡單方法是在初始化項目時安裝它們,或使用 npm install(或 yarn add)安裝包,這篇文章主要介紹了Vue3中如何使用SCSS編寫樣式,需要的朋友可以參考下
    2023-12-12
  • Vue手把手教你擼一個 beforeEnter 鉤子函數(shù)

    Vue手把手教你擼一個 beforeEnter 鉤子函數(shù)

    這篇文章主要介紹了Vue手把手教你擼一個 beforeEnter 鉤子函數(shù),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • vue實現(xiàn)底部菜單功能

    vue實現(xiàn)底部菜單功能

    本文通過實例代碼給大家介紹了vue實現(xiàn)底部菜單功能,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧
    2018-07-07
  • Vue動態(tài)生成el-checkbox點擊無法賦值的解決方法

    Vue動態(tài)生成el-checkbox點擊無法賦值的解決方法

    這篇文章主要給大家介紹了關于Vue動態(tài)生成el-checkbox點擊無法賦值的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-02-02
  • 用Vue.js方法創(chuàng)建模板并使用多個模板合成

    用Vue.js方法創(chuàng)建模板并使用多個模板合成

    在本篇文章中小編給大家分享了關于如何使用Vue.js方法創(chuàng)建模板并使用多個模板合成的相關知識點內(nèi)容,需要的朋友們學習下。
    2019-06-06
  • 詳解vue 計算屬性與方法跟偵聽器區(qū)別(面試考點)

    詳解vue 計算屬性與方法跟偵聽器區(qū)別(面試考點)

    這篇文章主要介紹了詳解vue 計算屬性與方法跟偵聽器區(qū)別(面試考點),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • vue 2.1.3 實時顯示當前時間,每秒更新的方法

    vue 2.1.3 實時顯示當前時間,每秒更新的方法

    今天小編就為大家分享一篇vue 2.1.3 實時顯示當前時間,每秒更新的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • vue實現(xiàn)列表倒計時

    vue實現(xiàn)列表倒計時

    這篇文章主要為大家詳細介紹了vue實現(xiàn)列表倒計時,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-09-09

最新評論