【发布时间】:2020-05-14 11:20:21
【问题描述】:
我有一个项目使用 Vue.js 2.4 + TypeScript + RequireJS 堆栈,需要升级到最新的 Vue.js。升级到 Vue.js 会破坏它,在根据文档进行更改后我无法修复此问题。
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Vue.js Scratchpad</title>
<link rel="icon" type="image/png" href="favicon.png">
<meta charset="UTF-8">
<script src="node_modules/requirejs/require.js"></script>
</head>
<body>
<h1>Vue.js Scratchpad</h1>
<div v-show="true" id="app" style="display: none">
<input v-model="user" autofocus="true" placeholder="Enter any user name">
<!--<button @click.prevent="onRenderComponent">Render</button>-->
<p v-show="loading">Loading...</p>
<router-view v-show="!loading" :user="user"
@loading="onLoading" @success="onLoaded"
@loading-error="onLoadingError"></router-view>
</div>
<script>
requirejs.config({
// By default load modules from `./`
baseUrl: '.',
/*
By default, modules would be loaded from {module}.js files (filename = module).
Specify {module} (or "{module}"): "{filepath_no_extension}" mapping if filename != module.
Specify fall-backs ([filepath1, filepath2]) for minimized / normal version consumption pattern.
*/
paths: {
// All third-party libraries must be included here. Use path fall-backs for minimized libs.
axios: "node_modules/axios/dist/axios.min",
vue: "node_modules/vue/dist/vue.min",
"vue-router": "node_modules/vue-router/dist/vue-router.min"
}
});
require(["app-pure-vue.js"]);
</script>
</body>
</html>
app-pure-vue.ts:
import * as Vue from "vue";
import * as VueRouter from "vue-router";
import { AxiosError } from "axios";
//region App components
import MessageComponent from "./messageComponent-pure-vue";
//endregion
Vue.use(VueRouter);
const routes = [
{
path: "/",
component: MessageComponent
}
];
const router = new VueRouter({
routes
});
const appOptions = {
router,
data() {
return {
user: null,
loading: false
};
},
methods: {
onLoading() {
// @ts-ignore
this.loading = true;
console.log("Loading...");
},
onLoaded() {
// @ts-ignore
this.loading = false;
console.log("Loaded.");
},
onLoadingError(error: AxiosError, serviceUrl: string) {
// @ts-ignore
this.loading = false;
console.log("Loading error for", serviceUrl, error.response);
}
}
};
new Vue(appOptions).$mount("#app");
messageComponent-pure-vue.ts:
import * as Vue from "vue";
import * as VueRouter from "vue-router";
import { AxiosError } from "axios";
//region App components
import MessageComponent from "./messageComponent-pure-vue";
//endregion
Vue.use(VueRouter);
const routes = [
{
path: "/",
component: MessageComponent
}
];
const router = new VueRouter({
routes
});
const appOptions = {
router,
data() {
return {
user: null,
loading: false
};
},
methods: {
onLoading() {
// @ts-ignore
this.loading = true;
console.log("Loading...");
},
onLoaded() {
// @ts-ignore
this.loading = false;
console.log("Loaded.");
},
onLoadingError(error: AxiosError, serviceUrl: string) {
// @ts-ignore
this.loading = false;
console.log("Loading error for", serviceUrl, error.response);
}
}
};
new Vue(appOptions).$mount("#app");
这个例子运行良好。现在,要升级到Vue.js 2.5.0 + 最新的vue-router,以下是所需的文档更改:
-
package.json:更新为"vue": "~2.5.0"+"vue-router": "~3.1.5" -
*.ts:import * as Vue from "vue";=>import Vue from "vue"; -
app-pure-vue.ts:import * as VueRouter from "vue-router";=>import VueRouter from "vue-router";
代码可以编译,但在messageComponent-pure-vue.ts 中的Vue.extend() 上崩溃:
Uncaught TypeError: Cannot read property 'extend' of undefined
at Object.<anonymous> (messageComponent-pure-vue.ts:5)
你能帮我解决这个问题吗?此处提供了最小可重现示例:https://github.com/DKroot/Scratchpad/tree/master/Client_Side/Vue.js-2.5。工作 2.4 代码位于 https://github.com/DKroot/Scratchpad/tree/master/Client_Side/Vue.js-2.4。
到目前为止我在这方面做了什么:
- 将问题隔离到 2.5.0 升级
- 仔细查看 2.5.0 发行说明 (https://github.com/vuejs/vue/releases/tag/v2.5.0) 和相应的博文 (https://medium.com/the-vue-point/upcoming-typescript-changes-in-vue-2-5-e9bd7e2ecf08)
- 审查了 2.5.0 中 TypeScript 声明的更改。出口有点过于复杂,我无法弄清楚根本原因可能是什么。
【问题讨论】:
-
Previously, we already recommend using ES-style imports (import Vue from ‘vue’) everywhere with “allowSyntheticDefaultImports”: true in tsconfig.json. The new typings will officially move to ES-style import/export syntax, so that config is no longer necessary, and users are required to use ES-style imports in all cases.所以你需要使用import Vue from 'vue',因为这是官方的ES风格的导入语法 -
@Ohgodwhy 这正是我对 2.5.0 所做的:从“vue”导入 Vue;从“vue-router”导入 VueRouter;这允许代码编译。但它在运行时会爆炸。
-
你安装的是什么版本的
vue-template-compiler? -
@Ohgodwhy 没有。这个最小示例中的所有依赖项都在
package.json中列出。 -
您能否验证您的
lock file中的vue-template-compiler版本是否与安装的Vue版本相匹配?
标签: typescript vue.js requirejs vue-router