[英]Vue.js Passing variable from one component to another (jQuery)
我有一個名為ShowComment
的組件和一個名為EditComment
的組件。
在ShowComment
中有一個變量this.CommentRecID
。 我想在組件EditComment
中使用這個變量。
問題是console.log(this.CommentRecID);
顯示該變量在EditComment
中未定義,但在ShowComment
中定義,但我不知道為什么它未定義:
我用它在EditComment
中“使用” this.CommentRecID
,但我不知道這是否是正確的方法,因為它與 jquery 相關:
import * as $ from "jquery";
import DatePicker from "vue2-datepicker";
export default {
props: ["CommentRecID"],
components: { DatePicker },
這是完整的ShowComment
組件: https://pastebin.com/fcy4PCq0
這是完整的Editcomment
組件: https://pastebin.com/uik7EwD1
我對 Vue.js 相當陌生。 有人知道如何解決這個問題嗎?
您不應同時使用jQuery
和Vue.js
。
您應該嘗試使用道具將數據從父母發送到孩子。
您可以將ShowComment
添加為EditComment
中的元素,如下所示:
<EditComment CommentRecID="this.CommentRecID" v-if="showEdit" />
並從editItem
方法切換showEdit
標志
editItem() {
this.showEdit = true
}
如果您想顯示模式,那么您的EditComment
組件可能在樹上,因此您可以使用EventBus或使用Vuex
。
看起來你已經在你的項目中使用Vuex
,所以添加一個存儲CommentRecID
的突變並以類似的方式使用它來顯示對話框。
您可以使用 Vue Props 輕松解決此問題,您必須將變量從父組件發送到子組件,請查看此PROPS
文檔,其不言自明:
Props Vuejs 文檔如果您在使用 props 時遇到問題,請告訴我
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.