vue 跳转html页面,vue项目实现页面的跳转

第一步、最大的index.html页面 添加如下代码

目的

http://localhost:8080/(等同于http://localhost:8080/index.html)是父页面

其他的页面都是http://localhost:8080/路径名;也是子页面

主要是构建 SPA (单页应用) 时,方便渲染你指定路由对应的组件。你可以 router-view 当做是一个容器,它渲染的组件是你使用 vue-router 指定的。

cee607ff492ad1f5d883a60ba86384f5.png

第二步、main.js文件

// The Vue build version to load with the `import` command

// (runtime-only or standalone) has been set in webpack.base.conf with an alias.

import Vue from 'vue'

import hd from './App'

import ElementUI from 'element-ui'

import 'element-ui/lib/theme-chalk/index.css'

import router from './router/router.js'

Vue.config.productionTip = false

Vue.use(ElementUI)

new Vue({

router,

el: '#app',

render:h => h(hd)

})

第三步、App.vue文件

第四步、router.js(本来是index.js,我改了名称)

我的目录结构如下

c2ad974c635401213e65f8055143f6e4.png

router.js代码如下

import Vue from 'vue'

import Router from 'vue-router'

import Head from '../components/head/Head'

import hmd from '../components/head/Zmd'

import ht from '../components/head/first'

import first from '../components/common/first'

import homd from '../components/home/Zmd'

Vue.use(Router)

export default new Router({

mode: 'history',

routes: [

{

path: '/head',

name: 'Head',

component: Head

},

{

path: '/first',

name: 'first',

component: first

},

{

path: '/hmd',

name: 'hmd',

component: hmd

}, {

path: '/homd',

name: 'homd',

component: homd

}, {

path: '/ht',

name: 'ht',

component: ht

}

, {

path: '/',

name: 'Head',

component: Head

}

]

})

第五步、实现路由跳转

由于我定义http://localhost:8080/就是head文件夹里面的Head.vue里面的内容

所以都是在这个Head.vue里面测试的

6937f9a0b6b953473921a94d80d8e8f2.png

components/head/Head.vue里面的内容如下

d5448a0cf077ad34a00c1109cb38602e.png

测试效果如下

1a7bc8c96d86ee25fd681e0dbc1a6e06.png

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
vue中,可以使用router来实现页面跳转。首先需要在项目中安装vue-router,然后在代码中引用和注册router。在Vue实例中,需要配置routes,即需要跳转页面的路径和组件。比如以下代码: ```javascript import Vue from 'vue'; import VueRouter from 'vue-router'; import Home from './components/Home.vue'; import About from './components/About.vue'; import Contact from './components/Contact.vue'; Vue.use(VueRouter); const routes = [ { path: '/', component: Home }, { path: '/about', component: About }, { path: '/contact', component: Contact } ]; const router = new VueRouter({ routes: routes }); new Vue({ router: router }).$mount('#app'); ``` 在上面的代码中,我们引入了VueRouter并将其注册,然后定义了三个组件Home、About和Contact。接下来,我们在routes中配置这三个页面的路径。最后,创建一个Vue实例并将router配置添加进去。 现在,我们就可以在Home、About和Contact组件中使用router-link来定义跳转到其他页面的链接,也可以使用router.push()方法来进行编程式导航,比如: ```html <!-- Home.vue --> <template> <div> <h1>Home Page</h1> <router-link to="/about">Go to about page</router-link> <button @click="goToContact">Go to contact page</button> </div> </template> <script> export default { methods: { goToContact() { this.$router.push('/contact'); } } }; </script> ``` 这样,当我们在页面中点击链接或者按钮时,就会跳转到对应的页面。需要注意的是,在使用router跳转页面时,不需要像传统的跳转一样带上文件拓展名,因为vue会根据配置的routes来匹配路径。如果需要跳转到一个外部html页面,可以在新窗口打开该页面,或者使用iframe来嵌入。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值