gpt4 book ai didi

vue.js - 如何在全局 vue 组件中调用方法?

转载 作者:搜寻专家 更新时间:2023-10-30 22:48:23 24 4
gpt4 key购买 nike

我有根 vue 组件 app.js:

...
const app = new Vue({
el: '#app',
...
methods: {
modalShow: function(target, id=null, message=null){
...
},
...
}
});

我有一个这样的子组件:

<template>
<div>
<ul>
<li>
<a href="#" class="thumbnail"
title="Add photo"
@click="modalShow('modal-add-photo', product.id)"
>
<span class="fa fa-plus fa-2x"></span>
</a>
</li>
</ul>
</div>
</template>

<script>

export default {
...
methods: {
...
}
}
</script>

modalShow 方法在根目录中。怎么才能从子调用呢?

如果现在执行上面的代码,我会得到以下错误:

[Vue warn]: Property or method "modalShow" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option.

最佳答案

modalShow 方法向下传递给子组件。

<child :modal-show="modalShow"></child>

export default {
props:["modalShow"]
}

然后就可以在child中使用这个方法了。

关于vue.js - 如何在全局 vue 组件中调用方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45094010/

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