【发布时间】:2019-07-17 13:54:21
【问题描述】:
我有一个使用 Vue csp 的 chrome 扩展
我这样实例化它:
class MyMethods {
testMe(){}
anotherFunction(){}
}
const methods = new MyMethods();
const app = new Vue({
el: '#app',
data: () => appData,
methods
});
它有效,我可以在我的模板(html 文件)中使用testMe() 和anotherFunction()。
<button v-on:click="testMe()">testMe()</button>
<button v-on:click="anotherFunction()">anotherFunction()</button>
但我想把它分成更多的类,比如MyMethodsX、MyMethodsY,所以我可以在需要时扩展它们。
但是我没有找到任何方法让 Vue 理解我不仅有一个对象,还有一个对象的对象。那就是我想对具有相同功能的函数进行分组,例如:
<button v-on:click="tests.testMe()">testMe()</button>
<button v-on:click="handling.anotherFunction()">anotherFunction()</button>
我试过了:
const app = new Vue({
el: '#app',
data: () => appData,
methods : {
methodsX: new MyMethods()
}
});
或
class MyWrapperClass() {
methodsX = new MyMethods()
}
const app = new Vue({
el: '#app',
data: () => appData,
methods : new MyWrapperClass()
});
但我不能调用testMe() 它总是说“无法读取未定义的属性'应用'”。
有没有办法做到这一点,以便我可以将新的命名空间/类/对象放入 Vue 应用程序的方法键中?
编辑:
我的最终目标是拥有这样的东西:
<div class="tests" v-if="testing">
<button :click="testing.test1()>Testing Uno</button>
<button :click="testing.test2()>Testing Dos</button>
</div>
<div class="handling" v-if="handling">
<button :click="handling.func1()>Handling Uno</button>
<button :click="handling.func2()>Handling Dos</button>
</div>
每个类别都是一个有自己的方法的类(可能还有测试:))
【问题讨论】:
-
在第一个示例中,您如何称呼
testMe()?每当我尝试调用它时,都会出现错误。 -
@zero298 我已经更新了我的问题:)
-
根据this answer,你可以做到,但我做不到
标签: vue.js