Mrcdh技术博客 Mrcdh技术博客
首页
  • Html5
  • Javascript
  • Nodejs
  • electron
  • Android
  • 微信公众号
  • 框架
  • 其他
  • Mysql
  • PHP
  • Python
  • java
  • Gulp
  • 其它
  • 网站
  • 资源
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Mrcdh

全栈开发小学生
首页
  • Html5
  • Javascript
  • Nodejs
  • electron
  • Android
  • 微信公众号
  • 框架
  • 其他
  • Mysql
  • PHP
  • Python
  • java
  • Gulp
  • 其它
  • 网站
  • 资源
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • element-ui

    • 基于 VUE 和 element-ui 的树形穿梭框组件 el-tree-transfer
    • element-ui常见问题
  • vue3

  • vue实现图片文件的显示和上传
  • composition-api
  • vue常用eslint配置模板
  • vue.config.js常用配置
  • vuepress-theme-vdoing本站使用主题
  • vue常见问题
    • Invalid Host header
    • 在 methods 中使用过滤器 filter
      • 访问全局过滤器
      • 访问本地过滤器
    • 全局引入sass变量
    • svg批量引入
      • 配置
      • 创建icons/index.js
      • 创建SvgIcon组件
      • 使用组件
    • 设置项目模板信息
    • vscode setup引入组件提示组件未使用等波浪线提示
    • vue-router访问相同地址报错
  • 常用插件

  • 《Vuejs》
mrcdh
2020-10-23
目录

vue常见问题

# Invalid Host header

npm run serve 出现 Invalid Host header

在vue.config.js中配置

module.exports = {
  devServer: {
    disableHostCheck: true
  }
}
1
2
3
4
5

# 在 methods 中使用过滤器 filter

# 访问全局过滤器

<template>
  <div>
    <span>过滤:{{'size' | dataFilter}}</span>
    <button @click="transformation">调用全局过滤器</button>
  </div>
</template>
<script>
  import Vue from 'vue';
  export default {
    methods: {
    // 使用过滤的方法
    transformation() {
      // 使用Vue.filter('过滤器名称') 方式获取全局中要使用的过滤器
      let dataFilter = Vue.filter('dataFilter');
      dataFilter('size'); // 对size进行数据过滤
    }
  }
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# 访问本地过滤器

<template>
  <div>
    <span>过滤:{{'size' | dataFilter}}</span>
    <button @click="transformation">调用本地过滤器</button>
  </div>
</template>
<script>
  export default {
    filters: {
      dataFilter(val) {
        console.log('本地过滤器',val)
      }
    },        
    methods: {
    // 使用过滤的方法
    transformation() {
      // 使用this.$options.filters[]方式获取本地过滤器      
      let dataFilter = this.$options.filters['dataFilter'];
      dataFilter('size'); // 对size进行数据过滤
    }
  }
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# 全局引入sass变量

使用到 sass-resources-loader 加载器

npm i -D sass-resources-loader
1
// vue.config.js
module.exports = {
  chainWebpack: (chain) => {
    const oneofsMap =  chain.module.rule('scss').oneOfs.store
    oneofsMap.forEach(item=>{
      item
      .use('sass-resources-loader')
      .loader('sass-resources-loader')
      .options({
        resources: './src/styles/variables.scss', 
      })
    })
  } 
} 
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# svg批量引入

npm install svg-sprite-loader -D
1

# 配置

module.exports = {  
  chainWebpack(config) {
    // 修改当前项目默认svg 配置,排除icons目录
    config.module.rule('svg')
      .exclude.add(resolve('./src/icons'))
    // 新增一个 rule:添加icons 里面svg
    config.module.rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('./src/icons')).end()
      .use('svg-sprite-loader')
        .loader('svg-sprite-loader')
        .options({symbolId: 'icon-[name]'})
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 创建icons/index.js

// icons/index.js
// webpack 创建一个以svg 目录为上下文的require函数
import Vue from 'vue'
import SvgIcon from '@components/SvgIcon.vue'
// 注册svg 组件
Vue.component(SvgIcon.name, SvgIcon)

const req=require.context('./svg',false,/\.svg$/)
req.keys().map(req);// keys() 会获取所有svg 文件
1
2
3
4
5
6
7
8
9

# 创建SvgIcon组件

<template>
  <svg :class="svgClass" aria-hidden="true">
    <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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

# 使用组件

<svg-icon icon-class="qiqiu" />
1

# 设置项目模板信息

// vue.config.js
module.exports = {
  ...
  configureWebpack: (config) => {
    // config.plugins.push(new BundleAnalyzerPlugin())
    config.plugins.forEach((val) => {
      if (val instanceof HtmlWebpackPlugin) {
        val.options.title = "项目名称"; // 修改输出文件名,<%= htmlWebpackPlugin.options.title %>
        // 指定模板
        val.options.template = path.resolve(__dirname, 'public', process.env.NODE_ENV === 'development' ? 'dev.html' : 'index.html')
      }
    });
  },
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# vscode setup引入组件提示组件未使用等波浪线提示

禁用Vetur使用Vue Language Features (Volar)

# vue-router访问相同地址报错

Avoided redundant navigation to current location:,多次执行会抛出NavigationDuplicated的警告错误是因为vue-router3版本之后 底层引入了promise。而我们通过声明式导航没有出现此类问题是因为vue-router底层已经处理好了。

在我们使用编程式导航时,通过给push或replace方法传递相应的成功和失败的回调函数可以捕获到当前的错误解决此问题

//编程式导航, 添加 ()=>{},()=>{} 解决此问题
this.$router.push({ name:'/' }, ()=>{}, ()=>{}) 
1
2

也可以修改原型方法达到一劳永逸

const originPush = VueRouter.prototype.push
VueRouter.prototype.push = function(location, resolve, reject) {
  if (resolve && reject) {
    originPush.call(this, location, resolve, reject)
  } else {
    originPush.call(this, location, () => {}, () => {})
  }
}
1
2
3
4
5
6
7
8
#vue
上次更新: 2023/09/22, 16:54:32
vuepress-theme-vdoing本站使用主题
pikaz-excel-js基于vue的excel导入导出插件

← vuepress-theme-vdoing本站使用主题 pikaz-excel-js基于vue的excel导入导出插件→

最近更新
01
uniapp常见问题
03-19
02
Vue3项目搭建
12-25
03
使用pnpm的monorepo组织项目
11-01
更多文章>
Theme by Vdoing | Copyright © 2020-2025 Mrcdh | 苏ICP备2020058908号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×