[英]How to get the view or component object using its name in Vue js?
Javascript代码:
var main = new Vue({
el: "#main",
data: {
currentView: "top",
},
});
var topComponent = Vue.component('top', Vue.extend({
template: "#top",
}));
现在,当我访问 main.currentView 时,我得到“顶部”。 但是有了这个字符串'top',我如何获得组件topComponent
?
来自VueJS 指南
尽管存在 props 和 events,但有时您可能仍需要直接访问 JavaScript 中的子组件。 为此,您必须使用
ref
为子组件分配一个引用 ID。 例如:
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>
这很简单。 您可以使用Vue.component
topComponent
var topComponent = Vue.component('top');
现在你有了topComponent
,你可以用它的方法做很多事情。
cid
options
super
extend
directive
filter
partial
transition
component
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.