vue实现点击链接,网页标题显示自定义内容

想要实现的效果
在这里插入图片描述
router文件夹下的index.js的写法:

import Vue from 'vue'
import Router from 'vue-router'
// import HelloWorld from '@/components/demo1'
import home from '@/components/home'
import about from '@/components/about'
import user from '@/components/user'
import homeMessage from '@/components/HomeMessage'
import homeNews from '@/components/HomeNews'
import VueRouter from 'vue-router'
Vue.use(Router)

 const routes=[
    {
      path: '',
      redirect: '/home',
    },
    {
      path: '/home',
      name: 'home',
      meta:{
        title:'首页'
      },
      component: home,
      children:[
        {
          path: 'homeMessage',
          name: 'homeMessage',
          component: homeMessage
        },
        {
          path: 'homeNews',
          name: 'homeNews',
          component: homeNews
        }
      ],
    },
    {
      path: '/about',
      name: 'about',
      component: about,
      meta:{
        title:'更多'
      },
    },
    {
      path: '/user',
      name: 'user',
      component: user,
      meta:{
        title:'用户'
      },
    }
  ]
  const router=new VueRouter({
    routes,
    mode:'history',
    linkActiveClass:'active'
  })
//前置钩子
router.beforeEach((to,from,next)=>{
  console.log(to.meta)
  document.title=to.meta.title
  next()
  console.log('前置钩子')
});
//后置钩子
router.afterEach((to,from)=>{
  document.title=to.meta.title
console.log('后置钩子')
})
export default router

其中需要:

  • 在跳转路径的配置里添加meta属性
  • 选择前置钩子或者后置钩子,将to里面的meta里的title字段赋值给document的title字段

ps:前置钩子需要表示是在跳转前发生,需要自己调用一下next()函数
后置钩子表示是在跳转后,直接获取to.meta.title赋值即可
调用顺序展示:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue.js 本身并没有提供制作图片缩略图的功能,但是我们可以利用第三方库或者插件来实现。以下是一种使用插件 vue-image-size 插件来制作图片缩略图的方法: 1. 安装 vue-image-size 插件: ``` npm install vue-image-size ``` 2. 在 Vue 组件中引入 vue-image-size 并使用: ``` <template> <div> <img :src="imageUrl" @load="onImgLoad" /> <img :src="thumbnailUrl" /> </div> </template> <script> import VueImageSize from 'vue-image-size'; export default { components: { VueImageSize, }, data() { return { imageUrl: 'https://example.com/image.jpg', thumbnailUrl: '', thumbnailWidth: 100, thumbnailHeight: 100, }; }, methods: { onImgLoad({ target }) { const { width, height } = target; this.thumbnailUrl = this.$imageSize.createThumbnail(this.imageUrl, this.thumbnailWidth, this.thumbnailHeight, { width, height }); }, }, }; </script> ``` 在上述代码中,我们首先引入了 vue-image-size 插件,然后在组件中使用 VueImageSize 组件。在 data 中定义了需要加载的原始图片地址 imageUrl,以及制作缩略图后的图片地址 thumbnailUrl 和缩略图宽高 thumbnailWidth 和 thumbnailHeight。在 onImgLoad 方法中,利用 $imageSize.createThumbnail 方法制作缩略图,并将生成的缩略图地址赋值给 thumbnailUrl 变量。最后在模板中渲染出原始图片和缩略图。 需要注意的是,vue-image-size 插件依赖于 canvas,所以在使用之前需要确保浏览器支持 canvas。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值