Vue + TypeScript 踩坑总结
<section id="nice" data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style="font-size: 16px; color: rgba(0, 0, 0, 1); padding: 10px; line-height: 1.6; word-spacing: 0; letter-spacing: 0; word-wrap: break-word; text-align: left; font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif"><h1 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(255, 87, 34, 1); font-size: 1.3em"><span style="display: inline-block; font-weight: normal; background: rgba(255, 87, 34, 1); color: rgba(255, 255, 255, 1); padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px">Vue+TS 踩坑记录与方案总结</span></h1><p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">点击跳转-原文地址</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em"></p><div class="table-of-contents" data-tool="mdnice编辑器"><ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc; font-size: 14px"><li>前言</li><li>整个 vue 项目的目录结构<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); font-size: 14px; list-style-type: square"><li>main.ts 中,提示`import App from './App.vue'`处,找不到 App.vue 这个模块</li><li>main.ts 中,往 Vue 的原型 prototype 上挂载全局变量</li></ul></li><li>全局组件注册</li><li>SFC 单 vue 文件组件的基本写法和结构</li><li>computed 计算属性的写法</li><li>watch 监听器的使用<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); font-size: 14px; list-style-type: square"><li>同一个 vue 页面中使用</li><li>父子两个 vue 页面传值后使用 watch 监听</li></ul></li><li>Watch 监听 store 中的数据改变</li><li>vue+ts 中,使用 filter 过滤器</li><li>自定义指令 过滤器【待补充】</li><li>watch 监听 router 的变化</li><li>main.ts 中注册路由导航守卫并在组件中使用路由钩子函数</li><li>父子传值 - 子组件修改触发父组件的方法执行</li><li>@Prop 默认参数</li><li>中央总线注册与使用【待解决】</li><li>vue + ts 中使用 vue-echarts</li><li>vue + ts 中使用 Element-ui</li><li>全局 scss 变量</li><li>alias 别名设置</li><li>请求接口的代理设置</li><li>本地服务域名修改</li><li>vue + ts 在 vscode 中的问题<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); font-size: 14px; list-style-type: square"><li>vue-cli 配置了 resolve alias 来声明的路径别名,在引用了 ts 后,vscode 会报错不能识别、模块查找失败:</li></ul></li></ul></div><p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em"></p>
<h2 data-tool="mdnice编辑器" style="margin: 20px 10px 0 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 24px; text-align: left"><span style="font-family: STHeitiSC-Light; font-size: 18px; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgba(255, 87, 34, 1)">前言</span></h2>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">vue 和 TypeScript 结合的情况下,很多写法和我们平时的写法都不太一样,这里总结我项目开发过程中遇到的问题和问题的解决方案<br>
<strong style="color: rgba(255, 87, 34, 1)">有些问题可能还没解决,欢迎各位大佬给与提点。</strong><br>
另外,使用本文前可以先看vue 官方文档关于 typescript 的使用讲解</p>
<h2 data-tool="mdnice编辑器" style="margin: 20px 10px 0 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 24px; text-align: left"><span style="font-family: STHeitiSC-Light; font-size: 18px; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgba(255, 87, 34, 1)">整个 vue 项目的目录结构</span></h2>
<ul data-tool="mdnice编辑器" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc; font-size: 14px">
<li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; font-size: 13px">大体用 vue-cli 创建的项目,结构基本不变。</section></li></ul>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">这里只写我后来为了解决问题改动的地方</p>
<h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 20px"><span style="font-size: 14px; color: rgba(255, 87, 34, 1); border-left: 3px solid; padding-left: 10px">main.ts 中,提示<code>import App from './App.vue'</code>处,找不到 App.vue 这个模块</span></h3>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">解决方案:
1、将 shims-vue.d.ts 文件一分为二。<br>
2、在 shims-vue.d.ts 文件同级目录下新建 vue.d.ts(名字不一定叫 vue,如 xxx.d.ts 也可以),然后此文件包含代码如下</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// vue.d.ts</span>
declare <span class="hljs-built_in" style="color: rgba(0, 134, 179, 1); line-height: 26px">module</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">'*.vue'</span> {
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span> Vue <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">from</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">'vue'</span>
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">export</span> <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">default</span> Vue
}
</code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">3、而原来的 shims-vue.d.ts 代码修改、新增如下:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// shims-vue.d.ts</span>
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span> Vue <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">from</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">'vue'</span>
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span> VueRouter, { Route } <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">from</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">'vue-router'</span>
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span> { Store } <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">from</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">'vuex'</span>
<p>declare <span class="hljs-built_in" style="color: rgba(0, 134, 179, 1); line-height: 26px">module</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">'vue/types/vue'</span> {<br>
interface Vue {<br>
<span class="hljs-attr" style="line-height: 26px">$router</span>: VueRouter;<br>
$route: Route;<br>
$store: Store<any>;<br>
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 以下是在main.ts中挂载到Vue.prototype上的变量</span><br>
$api: any;<br>
$mock: any;<br>
$configs: any;<br>
}<br>
}</p>
</code><p><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"></code></p></pre><p></p>
<h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 20px"><span style="font-size: 14px; color: rgba(255, 87, 34, 1); border-left: 3px solid; padding-left: 10px">main.ts 中,往 Vue 的原型 prototype 上挂载全局变量</span></h3>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">1、main.ts 配置</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// main.ts</span>
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span> api <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">from</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"./api/request"</span>;
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span> mock <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">from</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"./api/mock"</span>;
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span> configs <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">from</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"./utils/config"</span>;
</code><p><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px">Vue.prototype.<span class="math inline">\(api = api;
Vue.prototype.\)</span>mock = mock;<br>
Vue.prototype.$configs = configs;<br>
</code></p></pre><p></p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">2、shims-vue.d.ts 配置</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// shims-vue.d.ts 新增如下</span>
declare <span class="hljs-built_in" style="color: rgba(0, 134, 179, 1); line-height: 26px">module</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">'vue/types/vue'</span> {
interface Vue {
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// ...</span>
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 以下是在main.ts中挂载到Vue.prototype上的变量</span>
$api: any;
$mock: any;
$configs: any;
}
}
</code><p><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"></code></p></pre><p></p>
<h2 data-tool="mdnice编辑器" style="margin: 20px 10px 0 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 24px; text-align: left"><span style="font-family: STHeitiSC-Light; font-size: 18px; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgba(255, 87, 34, 1)">全局组件注册</span></h2>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">注册</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// main.ts</span>
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span> Page <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">from</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"@/components/page.vue"</span>;
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span> AllComponent <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">from</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"@/common/AllComponent.vue"</span>;
Vue.component(<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"Page"</span>, Page);
Vue.component(<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"all-component"</span>, AllComponent);
</code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">使用</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px">写法一:
<span class="hljs-tag" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px"><<span class="hljs-name" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px">Page</span> /></span>
写法二:
<span class="hljs-tag" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px"><<span class="hljs-name" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px">all-component</span> /></span>
</code></pre>
<h2 data-tool="mdnice编辑器" style="margin: 20px 10px 0 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 24px; text-align: left"><span style="font-family: STHeitiSC-Light; font-size: 18px; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgba(255, 87, 34, 1)">SFC 单 vue 文件组件的基本写法和结构</span></h2>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">一個简陋的 demo,展示 ts 下的 vue 文件中,对于相关功能的使用,重点关注<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(255, 93, 108, 1)"><Script></code>里的代码</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-tag" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px"><<span class="hljs-name" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px">template</span>></span>
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px"><!-- 结构示例,指令基础用法同vue --></span>
<span class="hljs-tag" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px"><<span class="hljs-name" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px">div</span> <span class="hljs-attr" style="color: rgba(0, 128, 128, 1); line-height: 26px">class</span>=<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"minos-system-setting"</span> <span class="hljs-attr" style="color: rgba(0, 128, 128, 1); line-height: 26px">v-if</span>=<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"hideHeader"</span>></span>
<span class="hljs-tag" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px"><<span class="hljs-name" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px">h3</span>></span>结构示例<span class="hljs-tag" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px"></<span class="hljs-name" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px">h3</span>></span>
<span class="hljs-tag" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px"><<span class="hljs-name" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px">span</span>></span>{{ selfKey1 }}<span class="hljs-tag" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px"></<span class="hljs-name" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px">span</span>></span>
<span class="hljs-tag" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px"><<span class="hljs-name" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px">ul</span>></span>
<span class="hljs-tag" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px"><<span class="hljs-name" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px">li</span> <span class="hljs-attr" style="color: rgba(0, 128, 128, 1); line-height: 26px">:key</span>=<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"item"</span> <span class="hljs-attr" style="color: rgba(0, 128, 128, 1); line-height: 26px">v-for</span>=<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"item in fatherKey"</span>></span>{{ item }}<span class="hljs-tag" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px"></<span class="hljs-name" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px">li</span>></span>
<span class="hljs-tag" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px"></<span class="hljs-name" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px">ul</span>></span>
<span class="hljs-tag" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px"><<span class="hljs-name" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px">button</span> @<span class="hljs-attr" style="color: rgba(0, 128, 128, 1); line-height: 26px">click</span>=<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"addText"</span>></span>追加文字<span class="hljs-tag" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px"></<span class="hljs-name" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px">button</span>></span>
<span class="hljs-tag" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px"><<span class="hljs-name" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px">AnotherVue</span>
<span class="hljs-attr" style="color: rgba(0, 128, 128, 1); line-height: 26px">:class</span>=<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"['default-class', selfKey1.length > 10 ? 'one' : 'two']"</span>
/></span>
<span class="hljs-tag" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px"></<span class="hljs-name" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px">div</span>></span>
<span class="hljs-tag" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px"></<span class="hljs-name" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px">template</span>></span>
</code><p><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-tag" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px"><<span class="hljs-name" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px">script</span> <span class="hljs-attr" style="color: rgba(0, 128, 128, 1); line-height: 26px">lang</span>=<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"ts"</span>></span><span class="javascript" style="line-height: 26px"><br>
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span> { Component, Vue, Prop, Watch } <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">from</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"vue-property-decorator"</span>;<br>
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span> { Route } <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">from</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"vue-router"</span>;<br>
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span> AnotherVue <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">from</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"@/components/AnotherVue.vue"</span>;<br>
@Component({<br>
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 组件注册</span><br>
components: {<br>
AnotherVue<br>
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 'another-vue': AnotherVue</span><br>
},<br>
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 过滤器</span><br>
filters: {<br>
filterFn1() {}<br>
},<br>
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 属性传递</span><br>
props: {<br>
<span class="hljs-attr" style="line-height: 26px">hideHeader</span>: {<br>
<span class="hljs-attr" style="line-height: 26px">type</span>: <span class="hljs-built_in" style="color: rgba(0, 134, 179, 1); line-height: 26px">Boolean</span>,<br>
<span class="hljs-attr" style="line-height: 26px">required</span>: <span class="hljs-literal" style="color: rgba(0, 128, 128, 1); line-height: 26px">false</span>,<br>
<span class="hljs-attr" style="line-height: 26px">default</span>: <span class="hljs-literal" style="color: rgba(0, 128, 128, 1); line-height: 26px">false</span> <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 默认属性的默认值</span><br>
}<br>
}<br>
})<br>
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">export</span> <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">default</span> <span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">class</span> <span class="hljs-title" style="color: rgba(68, 85, 136, 1); font-weight: bold; line-height: 26px">ComponentName</span> <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">extends</span> <span class="hljs-title" style="color: rgba(68, 85, 136, 1); font-weight: bold; line-height: 26px">Vue</span> </span>{<br>
@Prop({<br>
<span class="hljs-attr" style="line-height: 26px">type</span>: <span class="hljs-built_in" style="color: rgba(0, 134, 179, 1); line-height: 26px">Boolean</span>,<br>
<span class="hljs-attr" style="line-height: 26px">required</span>: <span class="hljs-literal" style="color: rgba(0, 128, 128, 1); line-height: 26px">false</span>,<br>
<span class="hljs-attr" style="line-height: 26px">default</span>: <span class="hljs-literal" style="color: rgba(0, 128, 128, 1); line-height: 26px">false</span> <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 默认属性的默认值</span><br>
})<br>
private hideHeader!: boolean | <span class="hljs-literal" style="color: rgba(0, 128, 128, 1); line-height: 26px">undefined</span>;<br>
@Prop() private fatherKey: string[]; <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 其他没有默认值的传值</span><br>
selfKey1: string = <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"自己的一个变量"</span>;<br>
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 生命周期</span><br>
created() {}<br>
mounted() {}<br>
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 计算属性</span><br>
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">get</span> computedKey() {<br>
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">return</span> <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">this</span>.selfKey1.length;<br>
}<br>
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 监听器</span><br>
@Watch(<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"computedKey"</span>)<br>
getcomputedKey(newVal) {<br>
<span class="hljs-built_in" style="color: rgba(0, 134, 179, 1); line-height: 26px">console</span>.log(newVal);<br>
}<br>
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 导航守卫函数</span><br>
private beforeRouteEnter(to: Route, <span class="hljs-attr" style="line-height: 26px">from</span>: Route, <span class="hljs-attr" style="line-height: 26px">next</span>: <span class="hljs-function" style="line-height: 26px"><span class="hljs-params" style="line-height: 26px">()</span> =></span> <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">void</span>): <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">void</span> {<br>
<span class="hljs-built_in" style="color: rgba(0, 134, 179, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"beforeRouteEnter"</span>, to, <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">from</span>, next);<br>
next();<br>
}<br>
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 方法</span><br>
addText() {<br>
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">this</span>.selfKey1 += <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">",追加文字!"</span>;<br>
}<br>
}<br>
</span><span class="hljs-tag" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px"></<span class="hljs-name" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px">script</span>></span><br>
<span class="hljs-tag" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px"><<span class="hljs-name" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px">style</span> <span class="hljs-attr" style="color: rgba(0, 128, 128, 1); line-height: 26px">lang</span>=<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"scss"</span> <span class="hljs-attr" style="color: rgba(0, 128, 128, 1); line-height: 26px">scoped</span>></span><span class="css" style="line-height: 26px"><br>
@<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"@/assets/styles/demo.scss"</span>;<br>
</span><span class="hljs-tag" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px"></<span class="hljs-name" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px">style</span>></span><br>
</code></p></pre><p></p>
<h2 data-tool="mdnice编辑器" style="margin: 20px 10px 0 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 24px; text-align: left"><span style="font-family: STHeitiSC-Light; font-size: 18px; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgba(255, 87, 34, 1)">computed 计算属性的写法</span></h2>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 计算属性</span>
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">get</span> computedKey() {
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">return</span> <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">this</span>.selfKey1.length
}
</code></pre>
<h2 data-tool="mdnice编辑器" style="margin: 20px 10px 0 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 24px; text-align: left"><span style="font-family: STHeitiSC-Light; font-size: 18px; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgba(255, 87, 34, 1)">watch 监听器的使用</span></h2>
<h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 20px"><span style="font-size: 14px; color: rgba(255, 87, 34, 1); border-left: 3px solid; padding-left: 10px">同一个 vue 页面中使用</span></h3>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span> { Component, Vue, Prop, Watch } <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">from</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">'vue-property-decorator'</span>
</code><p><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px">@Watch(<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">'boxHeight'</span>)<br>
getboxHeight(val) { <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// get+上边括号里的名字</span><br>
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// xxx</span><br>
}<br>
</code></p></pre><p></p>
<h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 20px"><span style="font-size: 14px; color: rgba(255, 87, 34, 1); border-left: 3px solid; padding-left: 10px">父子两个 vue 页面传值后使用 watch 监听</span></h3>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">子组件监听从父组件传过来的值
1、父组件用属性传值【前提是父组件引入子组件、注册并调用了】</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><ziZuJian :oneKey=<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"oneKeyObj"</span> />
</code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">2、子组件要使用的工具引入工作</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span> { Component, Vue, Prop, Watch } <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">from</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"vue-property-decorator"</span>;
</code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">3、子组件 Prop 接受</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">export</span> <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">default</span> <span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">class</span> <span class="hljs-title" style="color: rgba(68, 85, 136, 1); font-weight: bold; line-height: 26px">ZiZuJian</span> <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">extends</span> <span class="hljs-title" style="color: rgba(68, 85, 136, 1); font-weight: bold; line-height: 26px">Vue</span> </span>{
@Prop() private oneKey: object
}
</code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">4、子组件 Watch 监听</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px">@Watch(<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">'oneKey'</span>)
getoneKey(newVal,oldVal) {
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 监听成功后要做</span>
log(newVal)
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">this</span>.myfunction(newVal)
}
</code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">5、父组件(内部)改动值,会被子组件监听</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">export</span> <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">default</span> <span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">class</span> <span class="hljs-title" style="color: rgba(68, 85, 136, 1); font-weight: bold; line-height: 26px">FuZuJian</span> <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">extends</span> <span class="hljs-title" style="color: rgba(68, 85, 136, 1); font-weight: bold; line-height: 26px">Vue</span> </span>{
oneKeyObj = {}
...
mounted(){
$.ajax().then(<span class="hljs-function" style="line-height: 26px"><span class="hljs-params" style="line-height: 26px">()</span>=></span>{
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 适时情况下改动props传递的值,就会被子组件监听到改变</span>
oneKeyObj = {
<span class="hljs-attr" style="line-height: 26px">name</span> : <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">'测试'</span>
}
oneKeyObj.age = <span class="hljs-number" style="color: rgba(0, 128, 128, 1); line-height: 26px">18</span>
})
}
}
</code></pre>
<h2 data-tool="mdnice编辑器" style="margin: 20px 10px 0 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 24px; text-align: left"><span style="font-family: STHeitiSC-Light; font-size: 18px; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgba(255, 87, 34, 1)">Watch 监听 store 中的数据改变</span></h2>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">主要思路是计算属性获取 state 里的数据,watch 再监听计算属性</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span> { Component, Vue, Prop, Watch } <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">from</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">'vue-property-decorator'</span> <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 引入Watch</span>
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">get</span> stateSomeKey() { <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 计算属性</span>
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 监听state下的stateSomeKey对象中的keyName属性,return返回该值</span>
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">return</span> <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">this</span>[<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">'$store'</span>].state.stateSomeKey.keyName
}
@Watch(<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">'stateSomeKey'</span>) <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 与上边计算属性同名</span>
getstateSomeKey(val) { <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// get+上边括号里的名字</span>
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 监听到变化后,执行对应的内容</span>
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">this</span>.myFunction()
...
}
</code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">其中,第七行,监听器那里也可以這麽写</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px">@Watch(<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">'stateSomeKey'</span>) <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 与上边计算属性同名</span>
watchMenuState(val) { <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 这里可以这么写:或用watch+上边括号里的名字也可以(虽然不太确定为什么,只是代码这么写成功了)</span>
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 下同</span>
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// ...</span>
}
</code></pre>
<h2 data-tool="mdnice编辑器" style="margin: 20px 10px 0 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 24px; text-align: left"><span style="font-family: STHeitiSC-Light; font-size: 18px; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgba(255, 87, 34, 1)">vue+ts 中,使用 filter 过滤器</span></h2>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">定义:(在@Component 里边,写 filters,注意 s 单词)</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-tag" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px"><<span class="hljs-name" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px">script</span> <span class="hljs-attr" style="color: rgba(0, 128, 128, 1); line-height: 26px">lang</span>=<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"ts"</span>></span><span class="javascript" style="line-height: 26px">
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span> { Component, Vue, Prop } <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">from</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"vue-property-decorator"</span>;
@Component({
<span class="hljs-attr" style="line-height: 26px">filters</span>: {
filterValue(value) {
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">return</span> <span class="hljs-built_in" style="color: rgba(0, 134, 179, 1); line-height: 26px">Number</span>(value).toLocaleString();
}
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// otherFilterFn(value) { 其他filter示例</span>
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// return ...</span>
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// }</span>
},
<span class="hljs-attr" style="line-height: 26px">components</span>: {}
})
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">export</span> <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">default</span> <span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">class</span> <span class="hljs-title" style="color: rgba(68, 85, 136, 1); font-weight: bold; line-height: 26px">Container</span> <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">extends</span> <span class="hljs-title" style="color: rgba(68, 85, 136, 1); font-weight: bold; line-height: 26px">Vue</span> </span>{
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// ...</span>
}
</span><span class="hljs-tag" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px"></<span class="hljs-name" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px">script</span>></span>
</code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">使用:同之前,正常使用:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-tag" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px"><<span class="hljs-name" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px">span</span> <span class="hljs-attr" style="color: rgba(0, 128, 128, 1); line-height: 26px">v-if</span>=<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"showSpan"</span>></span>{{showValue | filterValue}}<span class="hljs-tag" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px"></<span class="hljs-name" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px">span</span>></span>
</code></pre>
<h2 data-tool="mdnice编辑器" style="margin: 20px 10px 0 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 24px; text-align: left"><span style="font-family: STHeitiSC-Light; font-size: 18px; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgba(255, 87, 34, 1)">自定义指令 过滤器【待补充】</span></h2>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 待补充</span>
</code></pre>
<h2 data-tool="mdnice编辑器" style="margin: 20px 10px 0 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 24px; text-align: left"><span style="font-family: STHeitiSC-Light; font-size: 18px; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgba(255, 87, 34, 1)">watch 监听 router 的变化</span></h2>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">1、shims-vue.d.ts 的设置</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// shims-vue.d.ts</span>
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span> Vue <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">from</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">'vue'</span>
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span> VueRouter, {Route} <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">from</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">'vue-router'</span>;
</code><p><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px">declare <span class="hljs-built_in" style="color: rgba(0, 134, 179, 1); line-height: 26px">module</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">'vue/types/vue'</span> {<br>
interface Vue {<br>
<span class="hljs-attr" style="line-height: 26px">$router</span>: VueRouter; <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 这表示this下有这个东西</span><br>
$route: Route;<br>
}<br>
}<br>
</code></p></pre><p></p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">2、main.ts 的设置</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// main.ts</span>
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span> { Component } <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">from</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"vue-class-component"</span>;
Vue.config.productionTip = <span class="hljs-literal" style="color: rgba(0, 128, 128, 1); line-height: 26px">false</span>;
Component.registerHooks([
<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"beforeRouteEnter"</span>, <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">//进入路由之前</span>
<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"beforeRouteLeave"</span>, <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">//离开路由之前</span>
<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"beforeRouteUpdate"</span>
]);
</code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">3、需要监听路由钩子的 SCF 组件:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-tag" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px"><<span class="hljs-name" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px">script</span> <span class="hljs-attr" style="color: rgba(0, 128, 128, 1); line-height: 26px">lang</span>=<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"ts"</span>></span><span class="javascript" style="line-height: 26px">
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// xxx.vue 的script标签内</span>
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span> { Component, Vue, Prop, Watch } <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">from</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"vue-property-decorator"</span>;
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span> { Route, RawLocation } <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">from</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">'vue-router'</span>;
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// # 下边两段,看你需要什么了:</span>
<p><span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 1/监听路由变化</span><br>
@Watch(<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">'$route'</span>,{ <span class="hljs-attr" style="line-height: 26px">immediate</span>: <span class="hljs-literal" style="color: rgba(0, 128, 128, 1); line-height: 26px">true</span> })<br>
private changeRouter(route: Route){<br>
<span class="hljs-built_in" style="color: rgba(0, 134, 179, 1); line-height: 26px">console</span>.log(route)<br>
}</p>
</span></code><p><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 2/定义路由钩子函数</span><br>
private beforeRouteEnter(to: Route, <span class="hljs-attr" style="line-height: 26px">from</span>: Route, <span class="hljs-attr" style="line-height: 26px">next</span>: <span class="hljs-function" style="line-height: 26px"><span class="hljs-params" style="line-height: 26px">()</span> =></span> <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">void</span>): <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">void</span> {<br>
<span class="hljs-built_in" style="color: rgba(0, 134, 179, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">'beforeRouteEnter'</span>, to, <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">from</span>, next)<br>
next(); <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 没有next将不会进入路由内部,跟vue文档用法一致</span><br>
}<br>
private beforeRouteUpdate(to: Route, <span class="hljs-attr" style="line-height: 26px">from</span>: Route, <span class="hljs-attr" style="line-height: 26px">next</span>: <span class="hljs-function" style="line-height: 26px"><span class="hljs-params" style="line-height: 26px">()</span> =></span> <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">void</span>): <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">void</span> {<br>
<span class="hljs-built_in" style="color: rgba(0, 134, 179, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">'beforeRouteUpdate'</span>); <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 暂时不生效,版本问题</span><br>
next();<br>
}<br>
private beforeRouteLeave(to: Route, <span class="hljs-attr" style="line-height: 26px">from</span>: Route, <span class="hljs-attr" style="line-height: 26px">next</span>: <span class="hljs-function" style="line-height: 26px"><span class="hljs-params" style="line-height: 26px">()</span> =></span> <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">void</span>): <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">void</span> {<br>
<span class="hljs-built_in" style="color: rgba(0, 134, 179, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">'beforeRouteLeave'</span>);<br>
next();<br>
}<br>
<span class="hljs-tag" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px"></<span class="hljs-name" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px">script</span>></span><br>
</code></p></pre><p></p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">监听路由的第二种写法 (如果只是想更新视图的话可以考虑监听路由)</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px">@Watch(<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">'$route'</span>)
routeWatch() {
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">this</span>.loadData();
}
</code></pre>
<h2 data-tool="mdnice编辑器" style="margin: 20px 10px 0 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 24px; text-align: left"><span style="font-family: STHeitiSC-Light; font-size: 18px; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgba(255, 87, 34, 1)">main.ts 中注册路由导航守卫并在组件中使用路由钩子函数</span></h2>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">基本同上<br>
1、shims-vue.d.ts 的设置</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// shims-vue.d.ts</span>
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span> Vue <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">from</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">'vue'</span>
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span> VueRouter, {Route} <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">from</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">'vue-router'</span>;
</code><p><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px">declare <span class="hljs-built_in" style="color: rgba(0, 134, 179, 1); line-height: 26px">module</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">'vue/types/vue'</span> {<br>
interface Vue {<br>
<span class="hljs-attr" style="line-height: 26px">$router</span>: VueRouter; <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 这表示this下有这个东西</span><br>
$route: Route;<br>
}<br>
}<br>
</code></p></pre><p></p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">2、main.ts 的设置</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// main.ts</span>
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span> { Component } <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">from</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"vue-class-component"</span>;
Component.registerHooks([
<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"beforeRouteEnter"</span>, <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">//进入路由之前</span>
<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"beforeRouteLeave"</span>, <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">//离开路由之前</span>
<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"beforeRouteUpdate"</span>
]);
</code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">3、需要监听路由钩子的 SCF 组件:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-tag" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px"><<span class="hljs-name" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px">script</span> <span class="hljs-attr" style="color: rgba(0, 128, 128, 1); line-height: 26px">lang</span>=<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"ts"</span>></span><span class="javascript" style="line-height: 26px">
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// xxx.vue 的script标签内</span>
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span> { Component, Vue, Prop, Watch } <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">from</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"vue-property-decorator"</span>;
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span> { Route, RawLocation } <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">from</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">'vue-router'</span>;
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// # 下边两段,看你需要什么了:</span>
<p><span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 1/监听路由变化</span><br>
@Watch(<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">'$route'</span>,{ <span class="hljs-attr" style="line-height: 26px">immediate</span>: <span class="hljs-literal" style="color: rgba(0, 128, 128, 1); line-height: 26px">true</span> })<br>
private changeRouter(route: Route){<br>
<span class="hljs-built_in" style="color: rgba(0, 134, 179, 1); line-height: 26px">console</span>.log(route)<br>
}</p>
</span></code><p><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 2/定义路由钩子函数</span><br>
private beforeRouteEnter(to: Route, <span class="hljs-attr" style="line-height: 26px">from</span>: Route, <span class="hljs-attr" style="line-height: 26px">next</span>: <span class="hljs-function" style="line-height: 26px"><span class="hljs-params" style="line-height: 26px">()</span> =></span> <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">void</span>): <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">void</span> {<br>
<span class="hljs-built_in" style="color: rgba(0, 134, 179, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">'beforeRouteEnter'</span>, to, <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">from</span>, next)<br>
next(); <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 没有next将不会进入路由内部,跟vue文档用法一致</span><br>
}<br>
private beforeRouteUpdate(to: Route, <span class="hljs-attr" style="line-height: 26px">from</span>: Route, <span class="hljs-attr" style="line-height: 26px">next</span>: <span class="hljs-function" style="line-height: 26px"><span class="hljs-params" style="line-height: 26px">()</span> =></span> <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">void</span>): <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">void</span> {<br>
<span class="hljs-built_in" style="color: rgba(0, 134, 179, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">'beforeRouteUpdate'</span>); <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 暂时不生效,版本问题</span><br>
next();<br>
}<br>
private beforeRouteLeave(to: Route, <span class="hljs-attr" style="line-height: 26px">from</span>: Route, <span class="hljs-attr" style="line-height: 26px">next</span>: <span class="hljs-function" style="line-height: 26px"><span class="hljs-params" style="line-height: 26px">()</span> =></span> <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">void</span>): <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">void</span> {<br>
<span class="hljs-built_in" style="color: rgba(0, 134, 179, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">'beforeRouteLeave'</span>);<br>
next();<br>
}<br>
<span class="hljs-tag" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px"></<span class="hljs-name" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px">script</span>></span><br>
</code></p></pre><p></p>
<h2 data-tool="mdnice编辑器" style="margin: 20px 10px 0 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 24px; text-align: left"><span style="font-family: STHeitiSC-Light; font-size: 18px; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgba(255, 87, 34, 1)">父子传值 - 子组件修改触发父组件的方法执行</span></h2>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em"><strong style="color: rgba(255, 87, 34, 1)">父组件内部:</strong><br>
1、调用子组件、并绑定传值:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-tag" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px"><<span class="hljs-name" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px">ZiZuJian</span> @<span class="hljs-attr" style="color: rgba(0, 128, 128, 1); line-height: 26px">chuanDiGuoQu</span>=<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"FuQinZiJiYong"</span>></span><span class="hljs-tag" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px"></<span class="hljs-name" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px">ZiZuJian</span>></span>
</code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">准备好一会会被子组件触发的函数:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px">FuQinZiJiYong(){
<span class="hljs-built_in" style="color: rgba(0, 134, 179, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">'我是父亲内部待被触发的方法'</span>)
}
</code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em"><strong style="color: rgba(255, 87, 34, 1)">子组件 ZiZuJian 内部</strong>在需要触发的地方执行$emit</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">export</span> <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">default</span> <span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">class</span> <span class="hljs-title" style="color: rgba(68, 85, 136, 1); font-weight: bold; line-height: 26px">Menu</span> <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">extends</span> <span class="hljs-title" style="color: rgba(68, 85, 136, 1); font-weight: bold; line-height: 26px">Vue</span> </span>{
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 在需要触发的地方,执行如下代码</span>
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">this</span>.$emit(<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">'chuanDiGuoQu'</span>, <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">''</span>)
}
</code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">最后还有另一种网友总结很麻烦的写法:参见地址</p>
<h2 data-tool="mdnice编辑器" style="margin: 20px 10px 0 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 24px; text-align: left"><span style="font-family: STHeitiSC-Light; font-size: 18px; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgba(255, 87, 34, 1)">@Prop 默认参数</span></h2>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">第一种:github 找到的 demo 这样。如下代码中<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(255, 93, 108, 1)">hideHeader</code>就是由默认参数的父组件传过来的属性</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">export</span> <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">default</span> <span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">class</span> <span class="hljs-title" style="color: rgba(68, 85, 136, 1); font-weight: bold; line-height: 26px">ComponentName</span> <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">extends</span> <span class="hljs-title" style="color: rgba(68, 85, 136, 1); font-weight: bold; line-height: 26px">Vue</span> </span>{
@Prop({
<span class="hljs-attr" style="line-height: 26px">type</span>: <span class="hljs-built_in" style="color: rgba(0, 134, 179, 1); line-height: 26px">Boolean</span>,
<span class="hljs-attr" style="line-height: 26px">required</span>: <span class="hljs-literal" style="color: rgba(0, 128, 128, 1); line-height: 26px">false</span>,
<span class="hljs-attr" style="line-height: 26px">default</span>: <span class="hljs-literal" style="color: rgba(0, 128, 128, 1); line-height: 26px">false</span> <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 默认属性的默认值</span>
})
private hideHeader!: boolean | <span class="hljs-literal" style="color: rgba(0, 128, 128, 1); line-height: 26px">undefined</span>
}
</code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">第二种:vue 原生的写法,并写到了@component 构造器中就好了:
如果不传值此函数默认就是 true,传 false 就是 false 了。并且能严格判断只能传 Boolean 类型。挺好。</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px">@Component({
<span class="hljs-attr" style="line-height: 26px">props</span>: {
<span class="hljs-attr" style="line-height: 26px">hideHeader</span>: {
<span class="hljs-attr" style="line-height: 26px">type</span>: <span class="hljs-built_in" style="color: rgba(0, 134, 179, 1); line-height: 26px">Boolean</span>,
<span class="hljs-attr" style="line-height: 26px">required</span>: <span class="hljs-literal" style="color: rgba(0, 128, 128, 1); line-height: 26px">false</span>,
<span class="hljs-attr" style="line-height: 26px">default</span>: <span class="hljs-literal" style="color: rgba(0, 128, 128, 1); line-height: 26px">false</span> <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 默认属性的默认值</span>
}
}
})
</code></pre>
<h2 data-tool="mdnice编辑器" style="margin: 20px 10px 0 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 24px; text-align: left"><span style="font-family: STHeitiSC-Light; font-size: 18px; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgba(255, 87, 34, 1)">中央总线注册与使用【待解决】</span></h2>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 待解决</span>
</code></pre>
<h2 data-tool="mdnice编辑器" style="margin: 20px 10px 0 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 24px; text-align: left"><span style="font-family: STHeitiSC-Light; font-size: 18px; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgba(255, 87, 34, 1)">vue + ts 中使用 vue-echarts</span></h2>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">安装</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px">npm i -S vue-echarts echarts
</code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">main.ts 中引入并注册</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// main.ts</span>
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 引用</span>
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span> ECharts <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">from</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"vue-echarts"</span>;
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 用到的模块要单独引用</span>
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"echarts/lib/chart/line"</span>; <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 线图为例,其他图一样</span>
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"echarts/lib/component/title.js"</span>; <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 标题</span>
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"echarts/lib/component/legend"</span>; <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 图例</span>
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"echarts/lib/component/tooltip"</span>; <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 提示框</span>
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"echarts/lib/component/toolbox"</span>; <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 工具(如下载功能与按钮)</span>
</code><p><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 注册</span><br>
Vue.component(<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"v-chart"</span>, ECharts);<br>
</code></p></pre><p></p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">vue.config.js 中设置</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// vue.config.js</span>
<span class="hljs-built_in" style="color: rgba(0, 134, 179, 1); line-height: 26px">module</span>.exports = {
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// For Vue CLI 3+, add vue-echarts and resize-detector into transpileDependencies in vue.config.js like this:</span>
transpileDependencies: [<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"vue-echarts"</span>, <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"resize-detector"</span>]
};
</code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">tsconfig.json 中也要设置</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px">// tsconfig.json
{
<span class="hljs-attr" style="line-height: 26px">"compilerOptions"</span>: {
<span class="hljs-attr" style="line-height: 26px">"types"</span>: [<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"webpack-env"</span>, <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"echarts"</span>]
}
}
</code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">SFC 应用</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-tag" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px"><<span class="hljs-name" style="color: rgba(0, 0, 128, 1); font-weight: normal; line-height: 26px">v-chart</span> <span class="hljs-attr" style="color: rgba(0, 128, 128, 1); line-height: 26px">:options</span>=<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"echartsOptions"</span> <span class="hljs-attr" style="color: rgba(0, 128, 128, 1); line-height: 26px">id</span>=<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"myCharts"</span> <span class="hljs-attr" style="color: rgba(0, 128, 128, 1); line-height: 26px">ref</span>=<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"myCharts"</span> /></span>
</code></pre>
<h2 data-tool="mdnice编辑器" style="margin: 20px 10px 0 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 24px; text-align: left"><span style="font-family: STHeitiSC-Light; font-size: 18px; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgba(255, 87, 34, 1)">vue + ts 中使用 Element-ui</span></h2>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// main.ts</span>
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span> ElementUI <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">from</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"element-ui"</span>;
Vue.use(ElementUI);
</code></pre>
<h2 data-tool="mdnice编辑器" style="margin: 20px 10px 0 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 24px; text-align: left"><span style="font-family: STHeitiSC-Light; font-size: 18px; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgba(255, 87, 34, 1)">全局 scss 变量</span></h2>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">在 assets/styles 下新建_variable.scss 文件,用于存放 scss 变量。<br>
然后再 vue.config.js 中设置全局变量</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// vue.config.js</span>
<span class="hljs-built_in" style="color: rgba(0, 134, 179, 1); line-height: 26px">module</span>.exports = {
<span class="hljs-attr" style="line-height: 26px">css</span>: {
<span class="hljs-attr" style="line-height: 26px">loaderOptions</span>: {
<span class="hljs-attr" style="line-height: 26px">sass</span>: {
<span class="hljs-attr" style="line-height: 26px">prependData</span>: <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">`
@import "@/assets/styles/_variable.scss";
`</span>
}
}
}
};
</code></pre>
<h2 data-tool="mdnice编辑器" style="margin: 20px 10px 0 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 24px; text-align: left"><span style="font-family: STHeitiSC-Light; font-size: 18px; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgba(255, 87, 34, 1)">alias 别名设置</span></h2>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">同时解决问题<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(255, 93, 108, 1)">alias 配置的路径别名,在 vscode 中报错模块查找失败</code>和问题<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(255, 93, 108, 1)">vue-cli 配置了 resolve alias 来声明的路径别名,在引用了 ts 后,vscode 会报错不能识别、模块查找失败</code>。其中,vscode 报错在 win 环境还需要一个插件安装,解决方案见下边
vue.config.js 配置</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// vue.config.js</span>
<span class="hljs-built_in" style="color: rgba(0, 134, 179, 1); line-height: 26px">module</span>.exports = {
<span class="hljs-attr" style="line-height: 26px">chainWebpack</span>: <span class="hljs-function" style="line-height: 26px"><span class="hljs-params" style="line-height: 26px">config</span> =></span> {
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 别名配置</span>
config.resolve.alias
.set(<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"comp"</span>, resolve(<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"src/components"</span>))
.set(<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"css"</span>, resolve(<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"src/assets/styles"</span>));
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// ...同上,路径核对好就行</span>
}
};
</code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">jsconfig.json 配置。注意这里的名字要和上边 set 后边的名字保持一致</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// jsconfig.json</span>
{
<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"compilerOptions"</span>: {
<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"paths"</span>: {
<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"@/*"</span>: [
<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"src/*"</span> <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 这个本来就有</span>
],
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 后边追加</span>
<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"comp/*"</span>: [
<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"src/components/*"</span>
],
<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"css/*"</span>: [
<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"src/assets/styles/*"</span>
],
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// ... 同上,路径核对好就行</span>
},
}
};
</code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">SCF 使用设定的别名</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// main.ts</span>
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span> MyError <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">from</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"view/error/Error.vue"</span>;
</code></pre>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">/* SCF单页中scss路径引用 */</span>
@<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span> <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"css/_variable.scss"</span>;
</code></pre>
<h2 data-tool="mdnice编辑器" style="margin: 20px 10px 0 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 24px; text-align: left"><span style="font-family: STHeitiSC-Light; font-size: 18px; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgba(255, 87, 34, 1)">请求接口的代理设置</span></h2>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">vue.config.js 配置</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// vue.config.js</span>
<span class="hljs-built_in" style="color: rgba(0, 134, 179, 1); line-height: 26px">module</span>.exports = {
<span class="hljs-attr" style="line-height: 26px">devServer</span>: {
<span class="hljs-attr" style="line-height: 26px">proxy</span>: {
<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"/api"</span>: {
<span class="hljs-attr" style="line-height: 26px">target</span>: <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"http://11.11.11.111/"</span>, <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 示例ip地址,也可以填域名,需要的是后端接口地址的相同部分</span>
changeOrigin: <span class="hljs-literal" style="color: rgba(0, 128, 128, 1); line-height: 26px">true</span>,
<span class="hljs-attr" style="line-height: 26px">pathRewrite</span>: {
<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"^/api"</span>: <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">""</span>
}
}
}
}
};
</code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">axios 请求地址时的写法:<br>
注意<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(255, 93, 108, 1)">/api</code>一定要有,且在路径的最前边,代替相同的路径。</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px">axios
.get(<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"/api/wo/de/di/zhi"</span>) <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 前边的'/api'一定要有,它代表的就是vue.config.js中proxy.target的路径</span>
.then(<span class="hljs-function" style="line-height: 26px"><span class="hljs-params" style="line-height: 26px">()</span> =></span> {
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 接口成功...</span>
});
</code></pre>
<h2 data-tool="mdnice编辑器" style="margin: 20px 10px 0 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 24px; text-align: left"><span style="font-family: STHeitiSC-Light; font-size: 18px; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgba(255, 87, 34, 1)">本地服务域名修改</span></h2>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">vue.config.js 配置</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// vue.config.js</span>
<span class="hljs-built_in" style="color: rgba(0, 134, 179, 1); line-height: 26px">module</span>.exports = {
<span class="hljs-attr" style="line-height: 26px">devServer</span>: {
<span class="hljs-attr" style="line-height: 26px">disableHostCheck</span>: <span class="hljs-literal" style="color: rgba(0, 128, 128, 1); line-height: 26px">true</span>, <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 用域名代替localhost,禁用主机检查</span>
host: <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"www.haha.com"</span>
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// 另外端口也可以在这里改,只不过我写到了package.json总,见下边</span>
}
};
</code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">package.json dev 命令的配置</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px">{
<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"scripts"</span>: {
<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"dev"</span>: <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"npm run serve"</span>,
<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"serve"</span>: <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"vue-cli-service serve --port 80 --open"</span>, <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px"># 端口设置为80,--open运行完毕后自动打开地址</span>
}
}
</code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">本地 host 配置</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px">127.0.0.1 www.haha.com <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px"># 这里注意和vue.config.js中的host的值对应</span>
</code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">此时,<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(255, 93, 108, 1)">npm run dev</code>成功后,浏览器跑项目输入地址<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(255, 93, 108, 1)">http://www.haha.com</code>即可</p>
<h2 data-tool="mdnice编辑器" style="margin: 20px 10px 0 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 24px; text-align: left"><span style="font-family: STHeitiSC-Light; font-size: 18px; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgba(255, 87, 34, 1)">vue + ts 在 vscode 中的问题</span></h2>
<h3 data-tool="mdnice编辑器" style="margin-top: 40px; margin-bottom: 20px; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 20px"><span style="font-size: 14px; color: rgba(255, 87, 34, 1); border-left: 3px solid; padding-left: 10px">vue-cli 配置了 resolve alias 来声明的路径别名,在引用了 ts 后,vscode 会报错不能识别、模块查找失败:</span></h3>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">1、扩展商店安装插件 - Path Intellisense</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">2、配置代码(vscode setting.json 中设置)</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px"><span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"path-intellisense.mappings"</span>: {
<span class="hljs-attr" style="line-height: 26px">"@"</span>: <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"\${workspaceRoot}/src"</span>
}
</code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">3、在 package.json 统计目录下创建 jsconfig.json 文件,并填入下边代码</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; border-radius: 8px">// jsconfig.json
{
<span class="hljs-attr" style="line-height: 26px">"compilerOptions"</span>: {
<span class="hljs-attr" style="line-height: 26px">"paths"</span>: {
<span class="hljs-attr" style="line-height: 26px">"@/*"</span>: [<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"src/*"</span>]
}
}
}
</code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: rgba(0, 0, 0, 1); margin: 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em">2019/12/09 ...持续更新中...</p>
</section>
</div>
<div id="MySignature" role="contentinfo">
越努力,越幸运;阿门。<br><br>
来源:https://www.cnblogs.com/padding1015/p/12013722.html
頁:
[1]