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 ...
随机推荐
- https ssl 总结
主要工作: 1)算法协商: 2)密钥交换: 3)身份认证: 4)数据通信: 1.2.3主要使用握手协议: 4使用记录协议. SSL协议可分为两层:记录协议.握手协议 SSL Record Protoc ...
- Python-通过configparser读写配置文件
Python读写配置文件: 1.创建配置文件(文件名以.conf或.ini结束的文件表示配置文件) 2.导入所需模块 OS, configparser >>> import os & ...
- solr的学习
1): http://archive.apache.org/dist/lucene/solr/ref-guide/ pdf下载地址 solr历史版本的下载:http://archive.apach ...
- JAVA 生成扫描条形码
声明:转载为个人学习收藏,如有侵权,请及时联系本人删除,转载地址:https://www.cnblogs.com/MariaWang/p/10837641.html 条形码是一种可视化.机器可读的数据 ...
- GETDATE()
定义和用法 GETDATE() 函数从 SQL Server 返回当前的时间和日期. 语法 GETDATE() 实例 例子 1 使用下面的 SELECT 语句: SELECT GETDATE() AS ...
- 用doxygen风格注释代码生成文档
目录 用doxygen风格注释代码生成文档 1. 说明 2. 具体操作 2.1 生成头部注释 2.2 安装doxygen 2.3 工程配置 3. 总结 用doxygen风格注释代码生成文档 1. 说明 ...
- 实体服务器安装centos7过程记录
一次在实体服务器安装centos 7的过程记录 第一次在实体服务器上面安装服务器(centos 7),在此记录安装过程中遇到的一些坑. 系统版本:CentOS Linux release 7.6.18 ...
- node源码详解(四)
本作品采用知识共享署名 4.0 国际许可协议进行许可.转载保留声明头部与原文链接https://luzeshu.com/blog/nodesource4 本博客同步在https://cnodejs.o ...
- hdu 3594 强连通好题仙人掌图,对自己的tarjan模板改下用这个
#include<stdio.h> #include<string.h> #define N 21000 struct node { int v,next; }bian[510 ...
- 0728MySQL数据库InnoDB存储引擎重做日志漫游REDOLOG,UNDOLOG
转自http://www.mysqlops.com/2012/04/06/innodb-log1.html 00 – Undo LogUndo Log 是为了实现事务的原子性,在MySQL数据库Inn ...