gpt4 book ai didi

javascript - 如何在 Vue js 中使用其名称获取 View 或组件对象?

转载 作者:数据小太阳 更新时间:2023-10-29 05:46:38 25 4
gpt4 key购买 nike

Javascript 代码:

    var main = new Vue({ 
el: "#main",
data: {
currentView: "top",
},
});

var topComponent = Vue.component('top', Vue.extend({
template: "#top",
}));

现在,当我访问 main.currentView 时,我得到“top”。但是对于这个字符串 'top',我如何获取组件 topComponent

最佳答案

来自VueJS guide

Despite the existence of props and events, sometimes you might still need to directly access a child component in JavaScript. To achieve this you have to assign a reference ID to the child component using ref. For example:

var parent = new Vue({ el: '#parent' })
// access child component instance
var child = parent.$refs.profile
<div id="parent">
<user-profile ref="profile"></user-profile>
</div>

关于javascript - 如何在 Vue js 中使用其名称获取 View 或组件对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23714737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com