I have a project using Vue.js 2.4 + TypeScript + RequireJS stack that needs to upgrade to the latest Vue.js. Upgrade to Vue.js breaks it and I could not fix this after making changes per documentation.
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");
This example works fine. Now, to upgrade to Vue.js 2.5.0 + the latest vue-router, here are the required documented changes:
package.json: update to"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";
The code would compile, but blow up on Vue.extend() in messageComponent-pure-vue.ts:
Uncaught TypeError: Cannot read property 'extend' of undefined
at Object.<anonymous> (messageComponent-pure-vue.ts:5)
Could you help me to fix this? The minimal reproducible example is available here: https://github.com/DKroot/Scratchpad/tree/master/Client_Side/Vue.js-2.5. The working 2.4 code is at https://github.com/DKroot/Scratchpad/tree/master/Client_Side/Vue.js-2.4.
What I did so far on this:
- Isolated the issue to 2.5.0 upgrade
- Carefully reviewed 2.5.0 release notes (https://github.com/vuejs/vue/releases/tag/v2.5.0) and the corresponding blog post (https://medium.com/the-vue-point/upcoming-typescript-changes-in-vue-2-5-e9bd7e2ecf08)
- Reviewed changes in TypeScript declarations in 2.5.0. The exports are a bit too complex for me to figure out what the root cause might be.
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.So you need to useimport Vue from 'vue', as that's the official ES-style import sytnaxvue-template-compilerdo you have installed?package.json.vue-template-compilerversion in yourlock filematches the installedVueversion?