在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
vue基于类的写法,和基于对象的写法并不一致。 import { Component, Emit, Inject, Model, Prop, Provide, Vue, Watch } from 'vue-property-decorator' 最主要的区别就是这里,组件的定义,参数的接受,方法的定义,等等。 路由监听用vue2的vue-cli创建项目,在 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中依然可以使用,因为使用了 解决方案: // 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>
效果图: 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 0 0; } ul { list-style-type: none; padding: 0; } li { display: inline-block; margin: 0 10px; } a { color: #42b983; } </style> 效果展示:
|
请发表评论