vue2和vue3选用的什么模块化标准
时间: 2023-08-20 08:04:05 浏览: 52
Vue 2.x 使用的是 CommonJS 模块化标准。这意味着在 Vue 2.x 中,你可以使用() 函数引入模块,使用 module.exports 语句暴露模块。
示例:
```javascript
// 引入模块
const Vue = require('vue');
// 暴露模块
module.exports = {
data() {
return {
message: 'Hello, Vue!'
};
},
methods: {
greet() {
console.log(this.message);
}
}
};
```
而 Vue 3.x 则开始采用了 ES6 模块化标准。这意味着你可以使用 import 语句引入模块,使用 export 语句暴露模块。
示例:
```javascript
// 引入模块
import { createApp } from 'vue';
// 暴露模块
export default {
data() {
return {
message: 'Hello, Vue 3!'
};
},
methods: {
greet() {
console.log(this.message);
}
}
};
```
需要注意的是,在 Vue 3.x 中,如果你想使用 Composition API,你需要通过 `import { createApp } from 'vue'` 引入 `createApp` 函数,而不是通过 `import Vue from 'vue'` 引入 Vue 对象。