使用 router-link 元素进行跳转
<router-link to="/example">Go to Example page</router-link>
使用 this.$router.push 方法进行跳转
this.$router.push('/example');
使用 this.$router.replace 方法进行跳转
this.$router.replace('/example');
可以使用 name 和 path 来定义和访问路由
设置 name 和 path 可以使得路由的访问更加方便,同时可以提高代码的可读性和可维护性。
// 使用 name 访问路由
this.$router.push({ name: 'example' });
// 使用 path 访问路由
this.$router.push({ path: '/example' });
在选择路由跳转方法时,应该根据具体的场景和需求来选择合适的方法
在模板中进行路由跳转,并且希望能够使用 Vue.js 的内置指令来处理事件,例如@click,那么建议使用 router-link 元素进行跳转。
在组件中进行路由跳转,并且希望能够在跳转时添加路由历史记录,以便用户可以通过浏览器的后退按钮回到之前的页面,那么建议使用 this.$router.push 方法进行跳转。
在组件中进行路由跳转,并且希望能够在跳转时替换当前的路由记录,以便用户无法通过浏览器的后退按钮回到之前的页面,那么建议使用 this.$router.replace 方法进行跳转。
总之,选择哪种路由跳转方法取决于具体需求和场景,这些方法都具有不同的特点和用途。
window.location.href ,router-link 元素,this.$router.push,this.$router.replace三者的区别:
window.location.href :
window.location.href 是 JavaScript 的一个全局对象,它提供了当前页面的 URL 地址,并且可以通过修改该属性的值来实现页面的跳转。例如:
window.location.href = ‘https://www.example.com’;
使用 window.location.href 进行页面跳转会刷新整个页面,这意味着之前的状态和数据都将被清除,而且用户在返回时需要重新加载所有内容。
router-link元素,this.$router.push 和 this.$router.replace:
相比之下,router-link元素,this.$router.push 和 this.$router.replace 是 Vue.js 中路由跳转的方法,它们不会刷新整个页面,只会部分更新视图,从而提高了页面的性能和用户体验。
this.$router.push 方法会将新路由添加到历史记录中。
this.$router.replace 方法会用新路由替换当前路由,不会添加到历史记录中。
例如:
// 使用 this.$router.push 跳转到另一个路由
this.$router.push(‘/example’);
// 使用 this.$router.replace 跳转到另一个路由
this.$router.replace(‘/example’);
因此,当您需要在 Vue.js 应用程序中进行路由跳转时,建议使用router-link元素,this.$router.push 或 this.$router.replace 方法,而不是直接使用 window.location.href。
路由策略配置
/src/router下配置路由策略
{
path: '/dispatchDict',
component: Layout,
hidden: true,
children: [
{
path: 'type/data',
component: (resolve) => require(['@/views/dispatch/dict/data'], resolve),
name: 'dispatchDict',
meta: { title: '调度字典数据', icon: '' }
}
]
},
router-link跳转
1)不带参数
<router-link :to="{name:'home'}">
<router-link :to="{path:'/home'}"> //name,path都行, 建议用name
// 注意:router-link中链接如果是'/'开始就是从根路由开始;如果不带'/',则从当前路由开始。
2)带params参数
<router-link :to="{name:'home', params: {id:10001}}">
// params传参数 (类似post)
// 路由配置 path: "/home/:id" 或者 path: "/home:id"
// 不配置path ,第一次可请求,刷新页面id会消失;配置path,刷新页面id会保留。
// html 取参 $route.params.id script 取参 this.$route.params.id
3)带query参数
<router-link :to="{name:'dispatchDict', query: {id:10001}}">
// query传参数 (类似get,url后面会显示参数)
// 路由可不配置
// html 取参 $route.query.id script 取参 this.$route.query.id
<!-- 带参数跳转 -->
<router-link :to="{path:'/dispatchDict/type/data',query:{setid:123456}}">
<button>点击跳转1</button>
</router-link>
this.$router.push()
1)不带参数
this.$router.push('/home')
this.$router.push({name:'home'})
this.$router.push({path:'/home'})
2)query传参
this.$router.push({name:'dispatchDict',query: {id:'10001'}})
this.$router.push({path:'/dispatchDict/type/data',query: {id:'10001'}})
// html 取参 $route.query.id script 取参 this.$route.query.id
3)params传参
this.$router.push({name:'dispatchDict',params: {id:'10001'}}) // 只能用 name
// 路由配置 path: "/home/:id" 或者 path: "/home:id" ,
// 不配置path ,第一次可请求,刷新页面id会消失
// 配置path,刷新页面id会保留
// html 取参 $route.params.id script 取参 this.$route.params.id
4)query和params区别
query类似get, 跳转之后页面url后面会拼接参数,类似?id=123456, 非重要性的可以这样传, 密码之类还是用params刷新页面id还在
params类似post, 跳转之后页面url后面不会拼接参数, 但是刷新页面id会消失。
this.$router.replace()
1)不带参数
this.$router.replace('/home')
this.$router.replace({name:'home'})
this.$router.replace({path:'/home'})
2)query传参
this.$router.replace({name:'dispatchDict',query: {id:'10001'}})
this.$router.replace({path:'/dispatchDict/type/data',query: {id:'10001'}})
// html 取参 $route.query.id script 取参 this.$route.query.id
3)params传参
this.$router.replace({name:'dispatchDict',params: {id:'10001'}}) // 只能用 name
// 路由配置 path: "/home/:id" 或者 path: "/home:id" ,
// 不配置path ,第一次可请求,刷新页面id会消失
// 配置path,刷新页面id会保留
// html 取参 $route.params.id script 取参 this.$route.params.id
this.$router.go(n)
<button @click="upPage">[上一页]</button>
<button @click="downPage">[下一页]</button>
upPage() {
this.$router.go(-1); // 后退一步记录,等同于 history.back()
},
downPage() {
this.$router.go(1); // 在浏览器记录中前进一步,等同于 history.forward()
}
this.$router.push()、 this.$router.replace()、 this.$router.go(n)三者区别
this.$router.push
跳转到指定url路径,并向history栈中添加一个记录,点击后退会返回到上一个页面。
this.$router.replace
跳转到指定url路径,但是history栈中不会有记录,点击返回会跳转到上个页面 (直接替换当前页面)。
this.$router.go(n)
向前或者向后跳转n个页面,n可为正整数或负整数。
目的页面动态刷新
参数不同,跳转到同一页面,会面临页面数据刷新问题。
watch: {
// 监视dictType变化
"$route.params.dictType": {
immediate: true,
handler() {
const dictType = this.$route.params.dictType;
this.getType(dictType);
},
},
},
//或者
watch: {
// 监视dictType变化
"$route.query.dictType": {
immediate: true,
handler() {
const dictType = this.$route.params.dictType;
this.getType(dictType);
},
},
},
<a>标签
a标签可以跳转外部链接,不能路由跳转
<a href="https://www.baidu.com"><button>点击跳转5</button></a>
<iframe>标签
想要在Vue应用中内嵌一个外部网页,可以使用<iframe>标签:
<template>
<div>
<!-- 在这里嵌入外部网页 -->
<iframe src="https://www.example.com" width="100%" height="500px" frameborder=iframe></iframe>
</div>
</template>
export default {
name: 'EmbeddedWebPage',
}
</script>
<style scoped>
/* 在这里添加样式 */
</style>
<iframe>标签的src属性设置为要嵌入的外部网页的URL
- 可以调整
width
和height
属性来设置iframe
的大小 frameborder
属性用于设置是否显示边框,设置为”0″表示不显示边框
uniapp使用iframe标签实现iframe高度自适应
实现代码:
- 使用JavaScript的方法来获取屏幕的高度,然后在CSS样式中使用。
- 代码示例:
- var screenHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
- document.documentElement.style.fontSize = (screenHeight / 10) + ‘px’;
- 上述代码中,将通过JavaScript获取到屏幕高度,然后将其除以10,并设置root节点的字体大小为结果值。这样,我们就可以使用rem单位计算所有元素的大小。
scrolling=”no” οnlοad=”this.height=this.contentWindow.document.documentElement.scrollHeight”
<iframe id="iframe_id" src="./static/map.html" frameborder="0" style="width:100%;" scrolling="no" onload="this.height=this.contentWindow.document.documentElement.scrollHeight"></iframe>
方法2:
实现代码: style=”width:100%;height: 100vh;”
- 使用CSS3的单位vh来表示当前视口(Viewport)的高度,1vh相当于视口高度的1%。
- 代码示例:
- height: 100vh;
<iframe :src="weburl" style="width:100%;height: 100vh;" scrolling="no" frameborder=0></iframe>
- 方法三:
- 使用CSS3的Media Query来根据设备屏幕的大小自适应样式。
- 代码示例:
- @media screen and (max-width: 768px) {
- /* CSS for screens smaller than 768px */
- }
- @media screen and (min-width: 769px) and (max-width: 1024px) {
- /* CSS for screens between 769px and 1024px */
- }
- @media screen and (min-width: 1025px) {
- /* CSS for screens larger than 1025px */
- }
- 通过Media Query,我们可以在不同尺寸的屏幕上适应不同的样式。
跳转传参方法
在Vue项目中,你可以使用路由(vue-router)来实现页面跳转并传递参数。下面是一些常用的方法:
使用路由的params属性:
1、在目标页面的路由配置中,设置props: true来启用参数传递。
2、在源页面中,使用this.$router.push方法跳转到目标页面,并传递参数。
3、在目标页面中,通过this.$route.params获取传递的参数。
使用Vue的props属性:
1、在目标组件中定义props属性,用于接收传递的参数。
2、在源页面中,使用this.$router.push方法跳转到目标页面,并在路由配置中设置props属性,将参数绑定到props上。
3、在目标组件中,通过props属性获取传递的参数。
使用Vuex进行状态管理:
在Vuex中定义一个状态(state),用于存储要传递的参数。
在源页面中,通过Vuex的mutations方法将参数添加到状态中。
在目标页面中,通过Vuex的getter方法获取传递的参数。
使用路由params属性的示例
下面是一个使用路由params属性的示例:
在目标页面的路由配置中设置props: true:
// router.js
{
path: '/target',
component: TargetComponent,
props: true // 启用参数传递
}
在源页面中使用this.$router.push方法跳转到目标页面并传递参数:
// source.vue
<template>
<button @click="navigate">跳转到目标页面</button>
</template>
<script>
export default {
methods: {
navigate() {
const param1 = 'Hello';
const param2 = 'World';
this.$router.push({ path: '/target', params: { param1, param2 } });
}
}
}
</script>
在目标页面中使用this.$route.params获取传递的参数:
// target.vue
<template>
<div>
<p>传递的参数1: {{ $route.params.param1 }}</p>
<p>传递的参数2: {{ $route.params.param2 }}</p>
</div>
</template>
接收页面传参数据的方法
在Vue项目中,可以使用以下方法接收页面传参数据:
1.使用props接收父组件传递的参数:
在子组件中定义props属性,接收父组件传递的参数。props属性需要使用v-bind绑定到父组件的对应属性上。
<template>
<div>
<p>{{ message }}</p>
</div>
</template>
<script>
export default {
props: ['message']
}
</script>
2.使用route对象获取路由参数:
在页面中使用route对象获取路由参数:在页面中使用route对象可以获取到路由参数,包括路径参数和查询参数。
<template>
<div>
<p>{{ $route.params.id }}</p>
<p>{{ $route.query.name }}</p>
</div>
</template>
3.使用axios等
HTTP客户端发送请求时,在URL中添加参数:使用HTTP客户端发送请求时,可以在URL中添加参数,然后在请求成功后的回调函数中获取到这些参数。
<template>
<div>
<button @click="getData">获取数据</button>
</div>
</template>
<script>
import axios from 'axios'
export default {
methods: {
getData() {
axios.get('/api/data?id=123&name=test')
.then(response => {
console.log(response.data)
})
}
}
}
</script>