vue2项目所有页面固定格式相同,内容不同时,封装组件

vue2项目所有页面固定格式,内容不同时,封装组件

新建json文件,把每页标题文字写到数组对象里

[
 {
  "title": "主页",
  "content": "这是主页"
  },
 {
  "title": "管理",
  "content": "这是管理"
  },
 {
  "title": "中心",
  "content": "这是中心"
  }
 ]

在父组件引入json文件,然后通过子组件传递的titile标题循环进行判断

<template>
  <div v-if="dataInfo" class="ExplainPage" :style="style">
    <h1>{{ dataInfo.title }}</h1>
    <p :title="dataInfo.content">{{ dataInfo.content }}
    </p>
  </div>
</template>
<script>
import DataJSON from './data.json'
export default {
 props: {
    title: {
      type: String,
      default: '主页'
    }
  },
 data() {
    return {
      dataInfo: null,
      style: {}
    }
  },
  //每页图片不同
   mounted() {
    if (!this.title) return
    DataJSON.forEach((e, i) => {
     if (e.title === this.title) {
        this.dataInfo = e
          this.style = {
         backgroundImage: 'url(' + require(`@/assets/image/top${i + 1}.png`) + ')',
          backgroundRepeat: 'no-repeat',
          backgroundPosition: 'left top',
          backgroundSize: '100% 100%'
        }
      }
    })
  }
  }
</script>
<style lang="scss" scoped>
//里面统一样式
</style>

每个子组件页面只需要props传参title值就可以

 <Page title="主页" />
 import ExplainPage from '@/components/Page'
  components: {Page },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值