vue把路由单独分离出来的方法
vue把路由单独分离出来的方法?这个问题可能是我们日常学习或工作经常见到的。希望通过这个问题能让你收获颇深。下面是小编给大家带来的参考内容,让我们一起来看看吧!
建立一个 router.js 文件
引入
import Vue from 'vue'import VueRouter from 'vue-router' import Home from '../components/home/home.vue'
然后注册
Vue.use(VueRouter);const router = new VueRouter({ mode : 'history', base: __dirname, routes: [ { path: historyUrl + '/', component: Home, name : '主页' },]}
最后暴露出云
export default router
在main.js 里面直接引入然后就可以用了
import router from './main/router.js'const app = new Vue({ router : router, watch : { '$route' (to,from,next){ //console.log(to) //路由监听 //console.log(from) } }, render : h => h(App)}).$mount('#app');
别的 js 文件如果要调用 router 方法,直接像 main.js 一样引入直接用就可以了
补充知识:vue.cli3设置单独路由页面全屏切换
不是全屏的时候
是全屏的时候
首先思想:获取当前路由页面的节点,对的节点操作定位,脱离文档流,top:0,;left:0;
1.用ref获取当前路由页面最大的div,也就是template包的第一个div,也可以是其他的
<template> <div ref="index"> //ref标识 <Title :refDome='refDome'></Title> </div></template>
2.如果要把节点从父组件传到子组件的话,在data里面定义一个值,然后在mounted赋值,在传给子组件(如果没有子组件直接跳过2,直接看3)
父组件
<template> <div ref="index"> <Title :refDome='refDome'></Title> //这里把data的值转给子组件Title </div></template><script>import Title from '../components/title'export default { components:{ Title }, data(){ return{ refDome:null } }, mounted(){ this.refDome = this.$refs.index //在这里给data赋值,记得要在mounted赋值 }}
子组件props接收值
<script>export default { props: ['refDome'],}</script>
3.然后在切换全屏的按钮上绑定@click事件,在点击当时操作节点,在data里面设置一个screen值为1,为了来回切换
// 点击切换全屏 handleFullScreen() { if (this.screen % 2 == 0) { this.refDome.style.position = 'static' this.screen++ } else { this.refDome.style.width = '100%' this.refDome.style.height = '100%' this.refDome.style.position = 'absolute' this.refDome.style.top = '0' this.refDome.style.left = '0' this.refDome.style.zIndex = '10' this.refDome.style.background = '#fff' this.screen++ } },
感谢各位的阅读!看完上述内容,你们对vue把路由单独分离出来的方法大概了解了吗?希望文章内容对大家有所帮助。如果想了解更多相关文章内容,欢迎关注亿速云行业资讯频道。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。