那些年前端工作遇到的问题(待整理)

16 篇文章 0 订阅
5 篇文章 0 订阅

js相关问题

1. 避免缓存

引入本地js加上Math.random()

vue相关问题

同一类名不同组件冲突

<style>里加上scoped

父子组件之间传值问题

Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop’s value. Prop being mutated: “imgUrl”

意思是父组件里面传到当前子组件里面的数据id。在当前子组件里面被重写了。
你在当前子组件里面重新定义一个新的变量接收修改后的值就可以啦

props: {
    imgUrl: {
      required: true,
      type: String
    }
  },
  data() {
    return {
      imgSrc:this.imgUrl
    };
  },

在这里插入图片描述

设置localhost和IP都可以访问

package.json文件里做如下修改:
在这里插入图片描述

"dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js --host 0.0.0.0"

Vue项目中使用svg图标

  • 安装依赖
npm install svg-sprite-loader --save-dev
  • 配置build文件夹中的webpack.base.conf.js,主要在两个地方添加代码,如下图所示

在这里插入图片描述

  • 在src/components下新建文件夹及文件SvgIcon/index.vue,index.vue中内容如下
<template>
  <svg :class="svgClass" aria-hidden="true" v-on="$listeners">
    <use :xlink:href="iconName"/>
  </svg>
</template>
 
<script>
  export default {
    name: 'SvgIcon',
    props: {
      iconClass: {
        type: String,
        required: true
      },
      className: {
        type: String,
        default: ''
      }
    },
    computed: {
      iconName() {
        return `#icon-${this.iconClass}`
      },
      svgClass() {
        if (this.className) {
          return 'svg-icon ' + this.className
        } else {
          return 'svg-icon'
        }
      }
    }
  }
</script>
 
<style scoped>
  .svg-icon {
    width: 1em;
    height: 1em;
    vertical-align: -0.15em;
    fill: currentColor;
    overflow: hidden;
  }
</style>
  • 在src下新建icons文件夹,及icons文件夹下svg文件夹、index.js文件,
  • svg文件夹里放入你的svg文件
  • index.js文件内容如下
import Vue from 'vue'
import SvgIcon from '@/components/SvgIcon'// svg组件
 
// register globally
Vue.component('svg-icon', SvgIcon)
 
const req = require.context('./svg', false, /\.svg$/)
const requireAll = requireContext => requireContext.keys().map(requireContext)
requireAll(req)
  • 在main.js中引入svg
    import './icons'
  • 使用
<svg-icon icon-class="test"></svg-icon>   //icon-class对应的是svg文件名

webpack搭建相关问题

  • ERROR in multi ./src/js/commission.js babel-polyfill

解决办法:重新安装babel-polyfill

  • glyphicons ModuleParseError: Module parse failed: Unexpected character ' '
    解决:
	{
       test: /\.(ttf|eot|svg|woff|woff2|otf)$/,
        use: 'url-loader'
    }, //处理字体文件的loader处理		

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值