vue2.0+typeScript项目
<p>https://segmentfault.com/a/1190000018720570#articleHeader15</p><p>https://segmentfault.com/a/1190000016423943</p>
<p>https://www.jianshu.com/p/fbcad30031c2</p>
<p> </p>
<p>我的github项目地址: https://github.com/zhaofangmei/vue-typescript-demo.git</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p>踩坑记:</p>
<p>1、配置完路由后,无法在组件中使用this.route和this. route和this.route和this.router(需要修改<code>src</code>目录下<span style="color: rgba(255, 0, 0, 1)"><code>shims-vue.d.ts</code></span>文件)</p>
<div class="cnblogs_code">
<pre>import Vue from 'vue'<span style="color: rgba(0, 0, 0, 1)">;
import VueRouter, { Route } from </span>'vue-router'<span style="color: rgba(0, 0, 0, 1)">;
declare module </span>"*.vue"<span style="color: rgba(0, 0, 0, 1)"> {
export </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)"> Vue;
}
declare module </span>'vue/types/vue'<span style="color: rgba(0, 0, 0, 1)"> {
interface Vue {
$router: VueRouter; </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">这表示this下有这个东西</span>
<span style="color: rgba(0, 0, 0, 1)"> $route: Route;
}
}</span></pre>
</div>
<p>2、在项目中使用vue+typescript的组合,但是mounte钩子没有触发</p>
<div class="cnblogs_code">
<pre><script lang="ts"><span style="color: rgba(0, 0, 0, 1)">
import { Vue, <span style="color: rgba(255, 0, 0, 1)">Component</span>, Watch } from </span>"vue-property-decorator"<span style="color: rgba(0, 0, 0, 1)">;
import { Route } from </span>"vue-router"<span style="color: rgba(0, 0, 0, 1)">;
<span style="color: rgba(255, 0, 0, 1)">@Component</span>
export </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)"> class App extends Vue {
private isShowNav: </span><span style="color: rgba(0, 0, 255, 1)">boolean</span> = <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
mounted(): </span><span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> {
console.log(</span>11111111111<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.routeChange(<span style="color: rgba(0, 0, 255, 1)">this</span>.$route, <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.$route);
}
@Watch(</span>"$route"<span style="color: rgba(0, 0, 0, 1)">)
routeChange(val: Route, oldVal: Route): </span><span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> {
console.log(val, oldVal);
}
}
</span></script></pre>
</div>
<p> 3、项目安装了element-ui的依赖包,main.ts全局引入了,element-ui样式就是没有显示,引入不了<span style="color: rgba(255, 0, 0, 1)">(vue.config.js的css.modules设置为false)-- </span><code><span class="hljs-comment">webpack 配置</span></code></p>
<p><span class="hljs-comment">推荐网址: https://cli.vuejs.org/zh/config/#css-modules</span></p>
<div class="cnblogs_code">
<pre>const path = require("path"<span style="color: rgba(0, 0, 0, 1)">);
const sourceMap </span>= process.env.NODE_ENV === "development"<span style="color: rgba(0, 0, 0, 1)">;
module.exports </span>=<span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 基本路径</span>
publicPath: "./"<span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 输出文件目录</span>
outputDir: "dist"<span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> eslint-loader 是否在保存的时候检查</span>
lintOnSave: <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> webpack配置</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> see https://github.com/vuejs/vue-cli/blob/dev/docs/webpack.md</span>
chainWebpack: () =><span style="color: rgba(0, 0, 0, 1)"> {},
configureWebpack: config </span>=><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (process.env.NODE_ENV === "production"<span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 为生产环境修改配置...</span>
config.mode = "production"<span style="color: rgba(0, 0, 0, 1)">;
} </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 为开发环境修改配置...</span>
config.mode = "development"<span style="color: rgba(0, 0, 0, 1)">;
}
Object.assign(config, {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 开发生产共同配置</span>
<span style="color: rgba(0, 0, 0, 1)"> resolve: {
extensions: [</span>".js", ".vue", ".json", ".ts", ".tsx"<span style="color: rgba(0, 0, 0, 1)">],
alias: {
vue$: </span>"vue/dist/vue.js"<span style="color: rgba(0, 0, 0, 1)">,
</span>"@": path.resolve(__dirname, "./src"<span style="color: rgba(0, 0, 0, 1)">),
</span>"@c": path.resolve(__dirname, "./src/components"<span style="color: rgba(0, 0, 0, 1)">),
less: path.resolve(__dirname, </span>"./src/less"<span style="color: rgba(0, 0, 0, 1)">),
}
}
});
},
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 生产环境是否生成 sourceMap 文件</span>
<span style="color: rgba(0, 0, 0, 1)">productionSourceMap: sourceMap,
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> css相关配置</span>
<span style="color: rgba(0, 0, 0, 1)">css: {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 是否使用css分离插件 ExtractTextPlugin</span>
extract: <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 开启 CSS source maps?</span>
sourceMap: <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> css预设器配置项</span>
<span style="color: rgba(0, 0, 0, 1)"> loaderOptions: {},
</span><span style="color: rgba(255, 0, 0, 1)"><strong>// 启用 CSS modules for all css / pre-processor files.
modules: false</strong></span><span style="color: rgba(0, 0, 0, 1)">
},
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> use thread-loader for babel & TS in production build</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> enabled by default if the machine has more than 1 cores</span>
parallel: require("os").cpus().length > 1<span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> PWA 插件相关配置</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> see https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-pwa</span>
<span style="color: rgba(0, 0, 0, 1)">pwa: {},
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> webpack-dev-server 相关配置</span>
<span style="color: rgba(0, 0, 0, 1)">devServer: {
open: process.platform </span>=== "darwin"<span style="color: rgba(0, 0, 0, 1)">,
host: </span>"localhost"<span style="color: rgba(0, 0, 0, 1)">,
port: </span>3001, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">8080,</span>
https: <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">,
hotOnly: </span><span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">,
proxy: {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置代理</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> proxy all requests starting with /api to jsonplaceholder</span>
"/api"<span style="color: rgba(0, 0, 0, 1)">: {
target: </span>"http://localhost:3000/"<span style="color: rgba(0, 0, 0, 1)">,
changeOrigin: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
ws: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
pathRewrite: {
</span>"^/api": ""<span style="color: rgba(0, 0, 0, 1)">
}
}
},
before: app </span>=><span style="color: rgba(0, 0, 0, 1)"> {}
},
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 第三方插件配置</span>
<span style="color: rgba(0, 0, 0, 1)">pluginOptions: {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ...</span>
<span style="color: rgba(0, 0, 0, 1)">}
};</span></pre>
</div>
<p> </p>
<p> </p>
<p><em id="__mceDel"> </em></p><br><br>
来源:https://www.cnblogs.com/zhaomeizi/p/11732597.html
頁:
[1]