这可能不是为什么有人想要将变量从一个 javascript 导出/导入到另一个的最佳示例,但希望它已经足够好了。
我有一个 javascript 文件,我在其中启动了一个变量,我们将这个变量称为 counter 和它所在的 javascript 文件CounterModel.js
。我还有一个 javascript 文件,它实际上对我的计数器做了一些名为CounterController.js
.
在我的 vue 文件中,我想启动我的计数器App.vue
并在Counter.vue
.
所以我在下面有这个设置,它给了我三个类似的警告,所以我一定做错了什么。所以我想问:
这里做错了什么?
输出
WARNING Compiled with 3 warnings 06:35:49
warning in ./src/App.vue?vue&type=script&lang=js&
"export 'default' (imported as 'counter') was not found in './api/CounterModel'
warning in ./src/components/counter/Counter.vue?vue&type=script&lang=js&
"export 'default' (imported as 'counterController') was not found in '../../api/CounterController'
warning in ./src/components/counter/Counter.vue?vue&type=script&lang=js&
"export 'default' (imported as 'counterController') was not found in '../../api/CounterController'
CounterModel.js
export let counter
async function initiateCounter () {
counter = 0
}
module.exports = {
initiateCounter
}
CounterController.js
import { counter } from './CounterModel'
async function incrementCounter () {
counter++
}
async function getCounter () {
return counter
}
module.exports = {
incrementCounter,
getCounter
}
应用程序.vue
<template>
<div>
<router-view></router-view>
</div>
</template>
<script>
import counter from '~/api/CounterModel'
export default {
name: 'App',
async created () {
await counter.initiateCounter()
}
}
</script>
计数器.vue
<template>
<div>
<button v-on:click="incrementCounter">Add 1</button>
{{ counter }}
</div>
</template>
<script>
import counterController from '~/api/CounterController'
export default {
name: 'Counter',
data () {
return {
counter: null
}
},
methods: {
incrementCounter: async function () {
counterController.incrementCounter()
},
getCounter: async function () {
this.counter = counterController.getCounter()
}
},
async created () {
await this.getCounter()
}
}
</script>