我想在文件之间共享 Vue 中的一种方法。我尝试了不同的建议(在谷歌之后)没有让它工作。我让 mixin 在同一个文件中工作 - 但不是从其他文件导入。
这是有效的(就在脚本标签下导入部分之后):
// import ipsecMixin from '../shared'
var ipsecMixin = {
methods: {
IpsecKodeRemote: function(
[kode here...]
....
export default {
name: 'MurSerit',
props: {
....
},
mixins: [ipsecMixin],
computed: {
但是后来我尝试将代码移动到外部文件(并导入,如您在上面示例中注释掉的部分中看到的那样):
var ipsecMixin = {
methods: {
IpsecKodeRemote: function(
[kode here...]
export { ipsecMixin }
我收到组件错误。
vue.runtime.esm.js?2b0e:587 [Vue warn]: Error in render: "TypeError: Cannot read property 'components' of undefined"
found in
---> <Modl2l> at src/components/Modl2l.vue
<Form> at src/components/Form.vue
<HelloWorld> at src/components/HelloWorld.vue
<Home> at src/views/Home.vue
<App> at src/App.vue
<Root>
为什么,以及如何解决它?
如果要
导出这样的显式变量:
export { ipsecMixin }
您还需要将其作为变量导入:
import { ipsecMixin } from '../shared'
您也可以使用默认导入/导出,如下所示:
// in shared.js
export default ipsecMixin = {
methods: {
IpsecKodeRemote: function(){},
}
}
// in your component file
import myIpSecMixin from '../shared'
请注意,在默认导入/导出中,您可以根据需要将导入命名为任何名称,但在导出显式变量名称时,还必须将其引用为相同的名称。
您可能想在此处查看如何使用 es6 导入
您需要
将ipsecMix导出为例如const
。
外部文件
export const ipsecMixin = {
methods: {
IpsecKodeRemote: function(
//code here...