亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將子組件中的文本字段鏈接到 VueJS 中的父組件的道具

將子組件中的文本字段鏈接到 VueJS 中的父組件的道具

冉冉說 2021-12-12 15:56:55
我有一個子組件通過事件向 VueJS 中的父組件發送數據。從父組件,我將數據(或嘗試路由數據...)路由到子組件的兄弟組件,并使用從子組件發送的數據創建新組件。我出于各種原因使用字典對數據進行分組,然后將字典推送到數組中。v-for 循環遍歷數組并使用在該數組中找到的數據填充前面提到的新組件。我可能不需要這樣做,但我就是這樣做的。我對替代品持開放態度??傊?,效果不是很好。到目前為止,我只能獲得需要出現在我想要的位置的三個字符串之一。我會在發布代碼后解釋更多。已經嘗試過:十幾個不同版本的代碼,包括在列表中創建一個簡單的 v-for 來完成這項工作,以及帶有/不帶有字典或數組的各種版本。在我對這個問題的研究中,我瀏覽了 VueJS 文檔,用谷歌搜索了一些東西,但一無所獲。在 App.vue 中(我試圖刪除所有不相關的東西):<template>    <div id="app">        <img alt="Vue logo" src="./assets/logo.png">        <TweetDeck v-on:messageFromTweetDeck="msgReceived($event)"/>        <!-- <ul>            <li v-for="(tweet, index) in tweets" :key="index">{{ tweet }}</li>        </ul>-->        <TwitterMsg v-for="(tweet, index) in tweets" :key="index"         :name="tweet.name" :handle="tweet.handle" tsp=3 :msg="tweet.tweet" />        <TwitterMsg name="aaa" handle='aaa'        tsp=50 msg="hey this is a message on twitter"/>        <input type="text" v-model="placeholderText"/>    </div></template><script>import TwitterMsg from './components/TwitterMsg.vue'import TweetDeck from './components/TweetDeck.vue'export default {    name: 'app',    components: {        TwitterMsg,        TweetDeck    },    data: function() {        return {            tweets: [],            message: "",            placeholderText: ""        }    },    methods: {        msgReceived(theTweet, name, handle) {            this.tweets.push({tweet: theTweet, name: name, handle: handle})        }    }}</script>
查看完整描述

1 回答

?
HUWWW

TA貢獻1874條經驗 獲得超12個贊

首先,您需要刪除$event參數


<TweetDeck v-on:messageFromTweetDeck="msgReceived"/>

其次,可以優化傳遞給父組件的數據格式:


sendTweet() {

  this.$emit("messageFromTweetDeck",

    { tweet: this.yourTweet, name: this.yourName, handle: this.yourHandle }

  );

}

然后修改你的msgReceived方法:


msgReceived(childData) {

  this.tweets.push(childData);

}


希望能幫到你:)


查看完整回答
反對 回復 2021-12-12
  • 1 回答
  • 0 關注
  • 140 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號