vue-cli3+typescript+router
vue基于类的写法,和基于对象的写法并不一致。
使用vue-cli3创建的项目,src目录下的文件结构并没有多大区别,store、router、app、view、components、aeests该有的还是有的。
但是,多了一个东西:vue-property-decorator,vue-property-decorator是vue-class-component的超集。
import { Component, Emit, Inject, Model, Prop, Provide, Vue, Watch } from 'vue-property-decorator'
最主要的区别就是这里,组件的定义,参数的接受,方法的定义,等等。
但是本文主要讲的是router的监听。
路由监听
用vue2的vue-cli创建项目,在src下有App.vue,main.js,其中如果要做路由权限控制,可以通过在main.js添加以下代码来控制:
import router from './router'
router.beforeEach((to, from, next) => {
/*如果需要登录,当前没有登录,直接跳转到登录页*/
if (to.meta.Auth && !store.state.loginStatus) {
return next({ name: 'Login', query: {path: to.name}})
}
next()
})
这个功能,在新版本的vue3中依然可以使用,因为使用了typescript,所以应该是main.ts文件。
但是如果要在组件内部使用路由监听,就遇到问题了,路由钩子beforeRouteEnter,beforeRouteLeave,beforeRouteUpdate不生效。
解决方案:
// main.ts
import Component from 'vue-class-component' Component.registerHooks([
'beforeRouteEnter',//进入路由之前
'beforeRouteLeave',//离开路由之前
'beforeRouteUpdate'
])
路由规则文件:
//router.ts
import Vue from 'vue';
import Router from 'vue-router';
import Home from './views/Home.vue'; Vue.use(Router); export default new Router({
mode: 'history',
base: process.env.BASE_URL,
routes: [
{
path: '/',
name: 'home',
component: Home,
},
{
path: '/about',
name: 'about',
// route level code-splitting
// this generates a separate chunk (about.[hash].js) for this route
// which is lazy-loaded when the route is visited.
component: () => import(/* webpackChunkName: "about" */ './views/About.vue'),
},
],
});
路由载入组件:
//About.vue <template>
<div class="about">
<h1>This is an about page</h1>
<h2 @click="click(10)">count:{{count}}</h2>
<p>getCount:{{getCount}}</p>
</div>
</template>
<script lang="ts">
import { Component, Emit, Inject, Model, Prop, Provide, Vue, Watch } from 'vue-property-decorator' @Component
export default class About extends Vue {
private count: number = ;
private num: number = ; private click(num: number) {
this.count = num + this.count;
this.num++
} // 相当于computed属性
get getCount() {
return this.count +
} beforeCreate() {
console.log('beforecreate');
}
created() {
console.log('created')
}
beforeMount() {
console.log('beforemounted');
}
mounted() {
console.log('mounted');
}
beforeRouteEnter(to: any, from: any, next: () => void): void {
console.log('beforeRouteEnter111');
next();
}
beforeRouteUpdate(to: any, from: any, next: () => void): void {
console.log('beforeRouteUpdate111');
next();
}
beforeRouteLeave(to: any, from: any, next: () => void): void {
console.log('beforeRouteLeave111');
next();
} @Watch('count') // 监听count
private aaa(val: any, oldval: any) {
console.log(val, oldval);
} @Watch('num') // 监听num
private bbb(a: number, b: number) {
console.log(a, b)
} } </script>
<style lang="less">
.about {
background-color: red;
h1 {
color: #fdfdfd;
}
}
</style>
效果图:

prop传值及component组件
// home.vue <template>
<div class="home">
<img alt="Vue logo" src="../assets/logo.png">
<HelloWorld :msg='msg' :title='title'/>
</div>
</template> <script lang="ts">
import { Component, Vue } from 'vue-property-decorator';
import HelloWorld from '@/components/HelloWorld.vue'; // @ is an alias to /src @Component({
components: {
HelloWorld,
},
})
export default class Home extends Vue {
private msg: String = "Welcome to Your Vue.js + TypeScript App"
private title: String = 'I am title'
} </script>
// helloworld.vue
<template>
<div class="hello">
<h2>title:{{title}}</h2>
<h1>msg:{{ msg }}</h1>
</div>
</template> <script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator'; @Component
export default class HelloWorld extends Vue {
@Prop() private msg !: string;
@Prop() private title !: string;
}
</script> <!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less">
h3 {
margin: 40px ;
}
ul {
list-style-type: none;
padding: ;
}
li {
display: inline-block;
margin: 10px;
}
a {
color: #42b983;
}
</style>
效果展示:

vue-cli3+typescript+router的更多相关文章
- Vue Cli3 TypeScript 搭建工程
Vue Cli3出来也一段时间了,我想尝试下Vue结合TypeScript搭建个工程,感受下Vue下用TS...网上有一篇讲的非常详细的教程 vue-cli3.0 搭建项目模版教程(ts+vuex+ ...
- 01-路由跳转 安装less this.$router.replace(path) 解决vue/cli3.0语法报错问题
2==解决vue2.0里面控制台包的一些语法错误. https://www.jianshu.com/p/5e0a1541418b 在build==>webpack.base.conf.j下注释掉 ...
- vue cli3 项目配置
[转]https://juejin.im/post/5c63afd56fb9a049b41cf5f4 基于vue-cli3.0快速构建vue项目 本章详细介绍使用vue-cli3.0来搭建项目. 本章 ...
- VUE CLI3.X 创建项目
Node.js环境搭建 Node.js基于V8引擎,可以让js代码脱离浏览器运行 Vue CLI3.0 需要Node.js 8.9或者更高版本. 用nvm或者nvm-windows在同一台电脑中管理多 ...
- 在 vue cli3 的项目中配置双服务,模拟 ajax 分页请求
最近安装了下vue cli3版本,与 cli 2 相比,文件少了,以前配置方法也不管用了.demo 中的大量的数据,需要做成 ajax 请求的方式来展示数据,因此,需要启动两个服务,一个用作前端请求, ...
- 安装VUE Cli3 框架方法
下面为大家介绍一下怎样安装 VUE Cli3的步骤 官网地址 https://cli.vuejs.org/zh/guide/installation.html 一.首先要检查一下是否安装node环 ...
- vue与TypeScript集成配置最简教程
https://blog.csdn.net/u014633852/article/details/73706459 https://segmentfault.com/a/119000001187808 ...
- vue cli3超详细创建多页面配置
1.首先按照vue cli3 给的入门文档下载个vue cli3 如果之前下载了vue cli2的要先卸载之前的 2.检查安装是否成功 3.ok,现在环境搭建好了,新建项目 vue create he ...
- Vue CLI3 关闭热替换后出现的warning
用vue cli3做项目的时候如果开启了typescript的严格模式,在dev server热替换的时候往往就会打出一大堆warning,严重的影响了编译效率.官方并没有提供关闭warning的ap ...
随机推荐
- 非传统题初探——AtCoder Practice Contest #B - インタラクティブ練習 (Interactive Sorting)
原题: Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement This is an interac ...
- 数据结构总结(UPDATING......)
目标: 1.栈........√ 2.队列......√ 3.堆.........× 4.并查集...× 栈: #define MAXN 65536 struct stack{ int sz[MAXN ...
- GOF23设计模式之适配器模式
GOF23设计模式之适配器模式 结构型模式: 核心作用:是从程序的结构上实现松耦合,从而可以扩大整体的类结构,用来解决更大的问题. 分类:适配器模式.代理模式.桥接模式.装饰模式.组合模式.外观模式. ...
- 用vue2.x注册一个全局的弹窗alert组件、confirm组件
一.在实际的开发当中,弹窗是少不了的,默认系统的弹窗样式太丑,难以满足项目的实际需求,所以需要自己定义弹窗组件,把弹窗组价定义为全局的,这样减少每次使用的时候引入麻烦,节省开发时间.本文将分享如何定义 ...
- WinForm 登录窗体 + 单实例运行
关于怎么在winform里增加登录窗体或者如何让winform程序单实例运行网上都很多例子. 然而两者结合起来呢? //Program.cs static class Program { public ...
- 线上MySQL慢查询现象案例--Impossible WHERE noticed after reading const tables
前言:2012年的笔记整理而得,发布个人博客,做备忘录使用. 背景:线上慢查询日志监控,得到如下的语句: 发现:select doc_text from t_wiki_doc_text w ...
- Mycat分表分库
一.Mycat介绍 Mycat 是一个开源的分布式数据库系统,是一个实现了 MySQL 协议的的Server,前端用户可以把它看作是一个数据库代理,用 MySQL 客户端工具和命令行访问,而其后端可以 ...
- Android学习心得(13) --- Android代码混淆(1)
我在博客上发表一些我的Android学习心得,希望对大家能有帮助. 这一篇我们讲述一下最新的ADT环境下怎样进行Android混淆 在新版本号的ADT创建项目时.混码的文件不再是proguard.cf ...
- How to pass external configuration properties to storm topology?
How to pass external configuration properties to storm topology? I want to pass some custom configur ...
- UVa 12525 Boxes and Stones (dp 博弈)
Boxes and Stones Paul and Carole like to play a game with S stones and B boxes numbered from 1 to B. ...