ホームページ  >  記事  >  ウェブフロントエンド  >  Vueコンポーネントでの親子コミュニケーションのチャットルーム例を詳しく解説

Vueコンポーネントでの親子コミュニケーションのチャットルーム例を詳しく解説

小云云
小云云オリジナル
2018-01-19 13:50:361665ブラウズ

この記事では主に、Vue コンポーネントでの親子コミュニケーションのための総合的な練習用チャット ルームの作成について詳しく紹介します。興味のある方は参考にしていただければ幸いです。


<!doctype html>
<html>
 <head>
 <meta charset="UTF-8">
 <title>组件父子间通信之综合练习</title>
 <script src="js/vue.js"></script>
 </head>
 <body>
 <p id="container">
  <p>{{msg}}</p>
  <chat-room></chat-room>
 </p>
 <script>

// 创建父组件
  Vue.component("chat-room",{
  //data属性中的chatList保存用户聊天信息
   data:function(){
    return{
     chatList:[]
    }
   },
   template:`
    <p>
     //假的聊天室
     <h1>假的聊天室</h1>
     <user-component userName="Rose"></user-component>
     <user-component userName="Jack"></user-component>
     //显示用户的聊天信息
     <ul>
      <li v-for="tmp in chatList">{{tmp}}</li>
     </ul>
    </p>
   `
  })
 //创建子组件 
  Vue.component("user-component",{
   props:["userName"],
   //通过v-model把用户输入的数据保存到userInput数组
   data:function(){
    return {
     userInput:[]
    }
   },
   methods:{
    //把用户输入的数据以及用户名label信息push给chatList数组
    sendChat:function(){
     this.$parent.chatList.push(this.userName+":"+this.userInput);
     //情况input框
     this.userInput =" ";
    }
   },
   template:`
    <p>
     <label>{{userName}}</label>
     <input type="text" v-model="userInput"/>
     <button @click="sendChat">发送</button>
    </p>
   `
  })
  new Vue({
   el:"#container",
   data:{
    msg:"Hello VueJs"
   }
  })
 </script>
 </body>
</html>

コンポーネント間通信に関する包括的な演習:
(プロパティは下、イベントは上)
2 つのコンポーネントがあります: チャット ルーム、ユーザー コンポーネント
ユーザー コンポーネントはラベル入力ボタンで構成されます
チャット ルームはユーザーコンポーネントとリストは 2 つで構成されます

①チャットルームでユーザーコンポーネントを呼び出してラベル名を指定します
②ユーザーコンポーネントでは、
ボタンをクリックすると現在のユーザーが入力した情報が表示されますがチャットルームコンポーネントに送信され、chat- ルームで受信したデータがリストに表示されます

コード:


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

<p id="container">
 <chat-room></chat-room>
</p>

<script>
 Vue.component(&#39;chat-room&#39;,{
  methods:{
   recvMsg:function(msg){
    console.log("在父组件中接收子组件传来的数据"+msg);
    this.chatList.push(msg);
   }
  },
 data: function () {
  return {
  chatList:[]
  }
 },
 template:`
  <p>
    <h1>假的聊天室</h1>
  <ul>
   <li v-for="tmp in chatList">
   {{tmp}}
   </li>
  </ul>
  <user-component userName="Lucy" @sendToFather="recvMsg"></user-component>
  <user-component userName="Merry" @sendToFather="recvMsg"></user-component>
  </p>
  `
 })

 Vue.component(&#39;user-component&#39;,{
 props:[&#39;userName&#39;],
 data: function () {
  return {
  userInput:&#39;&#39;
  }
 },
 methods:{
  sendToFather: function () {
  //触发toFatherEvent的事件,把input中
  //用户输入的数据发送
  this.$emit("sendToFather",this.userName+":"+this.userInput);
  }
 },
 template:`
  <p>
  <label>{{userName}}</label>
  <input type="text" v-model="userInput"/>
  <button @click="sendToFather">发送</button>
  </p>
  `
 })
 new Vue({
 el: &#39;#container&#39;,
  data: {
  msg: &#39;Hello Vue&#39;
  }
 })
</script>

</body>
</html>

関連推奨事項:

swooleとwebsocketの簡単なチャットルーム実装方法

PHP

html5の新技術ソケットでチャットルームを開発する方法 io

を使ってチャットルームを実装する方法。

以上がVueコンポーネントでの親子コミュニケーションのチャットルーム例を詳しく解説の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。