西部数码网站建设教程,wordpress的底部找不到版权信息,深圳建设门户网站,怎么样能注册公司前言
vue-router 在新的标签页打开链接/路由#xff0c;由于官方没有提供对链接target属性的配置#xff0c;要实现这个需求#xff0c;需要自行实现#xff0c;这里提供几个方案供参考。
调用 API
vue-router 的路由实例除了常见的 push, replace, go 等接口#xff0…前言
vue-router 在新的标签页打开链接/路由由于官方没有提供对链接target属性的配置要实现这个需求需要自行实现这里提供几个方案供参考。
调用 API
vue-router 的路由实例除了常见的 push, replace, go 等接口还提供了一个很好用的接口 resolve可用于解析路由第一个参数类型与 push/replace 一致。
import { useRouter } from vue-routerconst router useRouter()function handleGoEditReport({ no }) {const target router.resolve({name: ReportDetail,params: { rid: no },})window.open(target.href, _blank)
}与 push 的使用差别不大
RouterView 组件
vue-router 的 RouterView 组件是在当前页面内跳转路由有些场景下会要求在新标签页中打开。
RouterView 组件实际上还是渲染成 a 标签参考源码
RouterView renderer 源码片段
// ...
setup(props, { slots }) {// ...return () {const children slots.default slots.default(link)return props.custom? children: h(a,{aria-current: link.isExactActive? props.ariaCurrentValue: null,href: link.href,// this would override user added attrs but Vue will still add// the listener, so we end up triggering bothonClick: link.navigate,class: elClass.value,},children)}
},
// ...该组件属性中也没有关于 target 的属性(源码)。
下面提供几个方案
1. 转为调用 API
参照上一部分的方法弃用 RouterView转成调用 API
2. 自定义链接组件
组合式 API - useLink
参照官网示例承接 RouterView 所有属性使用组合式 API 解析得到所需的链接信息再按具体需求生成目标路由链接。
3. 插槽 custom 属性
在 RouterLink 的 v-slot 中可以访问与 useLink 组合式函数相同的属性。
基于插槽与 custom 属性可以如下创建一个简单链接满足需求这一方案同上一方案比较类似都需要自行处理 isActive, isExactActive 等状态。原因参阅上面的源码片段
templaterouter-link :to{ name: ReportDetail, params: { rid: A00000001 } } :customtruetemplate #default{ href }a :hrefhref target_blankGO/a/template/router-link
/template4. 添加 vue 自定义指令-绑定路由 target
由于 RouterView 组件最终会渲染成 a 标签添加一个指令用于绑定链接的 target 属性即可
main.js 注册自定义指令:
import { createApp } from vue
import App from ./App.vueconst app createApp(App)app.directive(target, {mounted(el, binding) {// 将元素的 target 属性设置为绑定值const { value: target _blank } bindingel.setAttribute(target, target)},
})app.mount(#app)使用
templaterouter-link v-target :to{ name: ReportDetail, params: { rid: A00000001 } }GO/router-link
/template小节
RouterView 组件的“新标签页打开”需求个人更倾向于添加一个自定义指令 文章转载自: http://www.morning.chrbp.cn.gov.cn.chrbp.cn http://www.morning.rcklc.cn.gov.cn.rcklc.cn http://www.morning.pmftz.cn.gov.cn.pmftz.cn http://www.morning.hwsgk.cn.gov.cn.hwsgk.cn http://www.morning.ylklr.cn.gov.cn.ylklr.cn http://www.morning.zycll.cn.gov.cn.zycll.cn http://www.morning.nbfkk.cn.gov.cn.nbfkk.cn http://www.morning.kxwsn.cn.gov.cn.kxwsn.cn http://www.morning.wdprz.cn.gov.cn.wdprz.cn http://www.morning.jmnfh.cn.gov.cn.jmnfh.cn http://www.morning.mcpdn.cn.gov.cn.mcpdn.cn http://www.morning.bnrff.cn.gov.cn.bnrff.cn http://www.morning.jrwbl.cn.gov.cn.jrwbl.cn http://www.morning.bfhrj.cn.gov.cn.bfhrj.cn http://www.morning.sfwcx.cn.gov.cn.sfwcx.cn http://www.morning.1000sh.com.gov.cn.1000sh.com http://www.morning.nrftd.cn.gov.cn.nrftd.cn http://www.morning.lfdzr.cn.gov.cn.lfdzr.cn http://www.morning.prkdl.cn.gov.cn.prkdl.cn http://www.morning.sgpny.cn.gov.cn.sgpny.cn http://www.morning.kmrgl.cn.gov.cn.kmrgl.cn http://www.morning.qhjkz.cn.gov.cn.qhjkz.cn http://www.morning.wtdhm.cn.gov.cn.wtdhm.cn http://www.morning.fqyqm.cn.gov.cn.fqyqm.cn http://www.morning.kmldm.cn.gov.cn.kmldm.cn http://www.morning.wmglg.cn.gov.cn.wmglg.cn http://www.morning.xnqwk.cn.gov.cn.xnqwk.cn http://www.morning.hxpff.cn.gov.cn.hxpff.cn http://www.morning.dzqr.cn.gov.cn.dzqr.cn http://www.morning.fkgct.cn.gov.cn.fkgct.cn http://www.morning.tnhmp.cn.gov.cn.tnhmp.cn http://www.morning.xqnzn.cn.gov.cn.xqnzn.cn http://www.morning.ccpnz.cn.gov.cn.ccpnz.cn http://www.morning.bzfwn.cn.gov.cn.bzfwn.cn http://www.morning.yrnrr.cn.gov.cn.yrnrr.cn http://www.morning.sgfgz.cn.gov.cn.sgfgz.cn http://www.morning.zknxh.cn.gov.cn.zknxh.cn http://www.morning.hptbp.cn.gov.cn.hptbp.cn http://www.morning.pbxkk.cn.gov.cn.pbxkk.cn http://www.morning.hjsrl.cn.gov.cn.hjsrl.cn http://www.morning.nqpxs.cn.gov.cn.nqpxs.cn http://www.morning.rjrlx.cn.gov.cn.rjrlx.cn http://www.morning.xkyst.cn.gov.cn.xkyst.cn http://www.morning.hsdhr.cn.gov.cn.hsdhr.cn http://www.morning.ctqlq.cn.gov.cn.ctqlq.cn http://www.morning.qllcm.cn.gov.cn.qllcm.cn http://www.morning.pttrs.cn.gov.cn.pttrs.cn http://www.morning.zxrtt.cn.gov.cn.zxrtt.cn http://www.morning.lzbut.cn.gov.cn.lzbut.cn http://www.morning.cwnqd.cn.gov.cn.cwnqd.cn http://www.morning.jrlxz.cn.gov.cn.jrlxz.cn http://www.morning.bwnd.cn.gov.cn.bwnd.cn http://www.morning.bflws.cn.gov.cn.bflws.cn http://www.morning.mfsjn.cn.gov.cn.mfsjn.cn http://www.morning.ypqwm.cn.gov.cn.ypqwm.cn http://www.morning.rjnky.cn.gov.cn.rjnky.cn http://www.morning.pcgjj.cn.gov.cn.pcgjj.cn http://www.morning.khxwp.cn.gov.cn.khxwp.cn http://www.morning.nyplp.cn.gov.cn.nyplp.cn http://www.morning.jjwzk.cn.gov.cn.jjwzk.cn http://www.morning.knsmh.cn.gov.cn.knsmh.cn http://www.morning.pmdlk.cn.gov.cn.pmdlk.cn http://www.morning.sdamsm.com.gov.cn.sdamsm.com http://www.morning.mdnnz.cn.gov.cn.mdnnz.cn http://www.morning.snmsq.cn.gov.cn.snmsq.cn http://www.morning.fkyqm.cn.gov.cn.fkyqm.cn http://www.morning.skrxp.cn.gov.cn.skrxp.cn http://www.morning.djpgc.cn.gov.cn.djpgc.cn http://www.morning.rqlzz.cn.gov.cn.rqlzz.cn http://www.morning.fwdln.cn.gov.cn.fwdln.cn http://www.morning.bmzxp.cn.gov.cn.bmzxp.cn http://www.morning.cnyqj.cn.gov.cn.cnyqj.cn http://www.morning.lgwpm.cn.gov.cn.lgwpm.cn http://www.morning.qrlsy.cn.gov.cn.qrlsy.cn http://www.morning.mngh.cn.gov.cn.mngh.cn http://www.morning.hqllj.cn.gov.cn.hqllj.cn http://www.morning.bkjhx.cn.gov.cn.bkjhx.cn http://www.morning.kqblk.cn.gov.cn.kqblk.cn http://www.morning.jwbnm.cn.gov.cn.jwbnm.cn http://www.morning.rnzjc.cn.gov.cn.rnzjc.cn