【vue】项目开发常见问题目录

0,页面初始化

<template>
    <div class="book">

    </div>
</template>

<script>
    export default {
        name: "book",
        data(){
            return{

            }
        },
        mounted() {

        },
        methods:{

        }
    }
</script>

<style scoped lang="less">
    .book{
        
    }
</style>

1, v-if 与 v-for 同时使用报错的问题

1.当在一个标签中,同时出现v-for和v-if的时候,会极大的消耗性能,并且vue会报错
2.使用v-for的时候,key缺少也会报错

解决方法:多加一个层级标签

 <div v-for="(item, index) in boxlist" :key="index">
     <div class="nevbox" v-if="index < 3">
     </div>
  </div>

2, 页面传参注意事项

params 传参注意事项,当页面刷新了是获取不到参数值

//前提:这个组件对应的路由配置
{
  //组件路径
  path: '/admin',
  //组件别名
  name: 'admin',
  //组件名
  component: Admin,
}
//传参
 this.$router.push({name:"Admin",params:{ nevId: type}});
//接收参
let nevId = this.$route.params.nevId;

query 传参,用 query 传参可以解决页面刷新参数消失问题

//传参
this.$router.push({name:"/admin",query:{nevId:type, name:item.name}})
//或者
this.$router.push(`/admin?nevId=${type}&orderId=${id}`);
//接收参
let nevId = this.$route.query.nevId;

3, Vue路由this.$router.push转跳同一个页面不刷新

  • 可以在父组件中对 标签(路由视图)绑定一个key,来给每个节点做一个唯一标识,这样就会更新DOM
<el-container>
      <header></header>
    <el-main >
        <router-view :key="$route.fullPath"></router-view>
    </el-main>
      <footer></footer>
</el-container>

4,NavigationDuplicated: Avoided redundant navigation to current location(路由跳转到当前页面报错)

  • 不影响使用,但属于警告
  • 只需要在你的router.js也就是路由配置文件下,添加上即可
  • 注意Router是你实例化的路由对象,版本不同有可能是VueRouter
    在这里插入图片描述
//获取原型对象上的push函数
const originalPush = Router.prototype.push
//修改原型对象中的push方法
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

5,vue富文本图片超出

  • 将拿到的富文本做一下处理
this.richText = res.data.data.replace(/\<img/gi,`<img style="width:100%;" mode="widthFix"`);

6,css背景图片变量使用

 background-image: url("~@/assets/img/ic_image7.png");
 background-size: 100% 100%;

7,css中数值计算

 height:  calc(100vh - 78px);

8,行内样式多个动态变量书写,逗号隔开

  • keybottonm和Keydibu是data中设置的变量
:style="{'padding-bottom':keybottonm+'rpx','bottom':Keydibu+'px'}"

9,监听游览器最大化和最小化

  onLoad(){
     window.onresize = function () {
            // 浏览器变化时todo
            that.isnum = that.isFullScreen();
            //全屏
            if (that.isnum) {
              history.go(0);
            } else {
            	//如果不是最大化,且开关开启的情况下执行代码
              if (that.isbig == 1) {
                
                return;
              } else {
                history.go(0);
              }
            }
     };
	},
  methods: {
    isFullScreen() {
      console.log(window.outerHeight, screen.availHeight);
      if (window.outerHeight === screen.availHeight) {
        if (window.outerWidth === screen.availWidth) {
          return true; // 全屏
        }
      }
      return false; // 不是全屏
    },
  }

10. 刷新当前页面会弹出提示框怎样将这个提示框去掉

window.onbeforeunload = function() {
    var n = window.event.screenX - window.screenLeft;
    var b = n > document.documentElement.scrollWidth-20;
    if(b && window.event.clientY < 0 || window.event.altKey){

    }else{
        location.href=location.href;  //当前页面等于当前页面
    }
 }

11. element 的轮播图左右箭头自定义

  • 第一步:ref=“cardShow”,定义一个ref
<div class="blockall">
      <el-carousel height="154px" :interval="3000" arrow="never" ref="cardShow" :autoplay="false">
        <el-carousel-item v-for="item in 4" :key="item">
          <div class="services_allbar">
            <div class="services_bar" v-for="e in 7" :key="e">
              <img src="./assets/Ellipse 5.png">
              <p>嘻嘻嘻嘻嘻嘻嘻嘻</p>
            </div>
          </div>
        </el-carousel-item>
      </el-carousel>
      <div class="design__left" @click="arrowClick('prev')">
        <img src="./assets/left.png">
      </div>
      <div class="design__right " @click="arrowClick('next')">
        <img src="./assets/right.png">
      </div>
  </div>
.blockall {
  width: 100%;
  position: relative;
  .design__left {
    position: absolute;
    top: 50%;
    transform: translate(0, -50%);
    left: -54px;
    width: 38px;
    height: 38px;
  }

  .design__right {
    position: absolute;
    top: 50%;
    transform: translate(0, -50%);
    right: -54px;
    width: 38px;
    height: 38px;
  }
}
  • 步骤二 js
// 下一页
arrowClick(val) {
      if (val === 'next') {
        this.$refs.cardShow.next()
      } else {
        this.$refs.cardShow.prev()
      }
},

12. 更换本页面所有的img图片,替换域名

 if (process.env.NODE_ENV === 'development') {
      setTimeout(() => {
        document.querySelectorAll('img').forEach((img) => {
          if (/https/.test(img.src)) {
            img.src = img.src.replace('https://xxxxxx.cn', '');
          }
        });
      }, 500);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值