vue开发中遇到的问题

た 入场券 2022-06-04 00:05 1231阅读 0赞

1、(1)Cannot find module ‘electron’
解决方案:cmd中输入:npm install --save-dev electron
(2)后来还差cfonts、webpack-dev-server、webpack-hot-middleware、babili-webpack-plugin、eslint-friendly-formatter、copy-webpack-plugin、extract-text-webpack-plugin、html-webpack-plugin,同样安装依赖。
2、屏幕自适应
(1)

  1. npm i lib-flexible --save
  2. npm i px2rem-loader --save-dev

(2)build/utils.js

  1. const cssLoader = {
  2. loader: 'css-loader',
  3. options: {
  4. sourceMap: options.sourceMap
  5. }
  6. }
  7. const px2remLoader = {
  8. loader: 'px2rem-loader',
  9. options: {
  10. remUnit: 75 // 这个参数值是通过psd设计稿的宽度/10来计算的,这里是以750
  11. }
  12. }
  13. function generateLoaders (loader, loaderOptions) {
  14. const loaders = options.usePostCSS ? [cssLoader, px2remLoader, postcssLoader] : [cssLoader, px2remLoader]

(3)main.js
import 'lib-flexible';
(4)index.html
viewport设置成width=device-width,initial-scale=1.0
(5)字体想显示成14px,则设计图28px,代码是28px。
3、为什么你做的H5开屏那么慢?H5首屏秒开方案探讨
https://mp.weixin.qq.com/s/ye1CeIjlfs9VSUab3gQI5g
4、浏览器通过写ip:端口号无法打开,提示“不允许访问”。
(1)config-index.js-dev-host,改成0.0.0.0
这样浏览器中输入:http://192.168.11.102:8081/
就能打开了。
5、数据存储
‘vuex’ and ‘vuex-persist’
6、如果是从别人那儿下载下来的项目,自己想要打开
(1)npm run dev 尝试启动项目
报错:Error: Cannot find module ‘express’
解决:看package.json里有没有express的依赖项,有的话直接npm install,没有的话 npm install express —save
(2)Cannot find module ‘webpack-merge’,应该是很多依赖没有安装,所以都安装一下
npm install —save
(3)报错:stylus-loader@2.5.1 requires a peer of stylus@>=0.52.4 but none was installed.
解决:npm install stylus —save
(4)config-index.js-dev-port改个端口号,就能在指定端口打开项目。
6、项目发布到线上后
报错:webpackJsonp is not defined
解决:build-webpack.pro.conf.js

  1. new HtmlWebpackPlugin({
  2. }),

加上了

  1. chunks: ['manifest', 'vendor', 'app'],

,即

  1. new HtmlWebpackPlugin({
  2. filename: config.build.index,
  3. template: 'index.html',
  4. inject: true,
  5. minify: {
  6. removeComments: true,
  7. collapseWhitespace: true,
  8. removeAttributeQuotes: true
  9. // more options:
  10. // https://github.com/kangax/html-minifier#options-quick-reference
  11. },
  12. chunks: ['manifest', 'vendor', 'app'],
  13. // necessary to consistently work with multiple chunks via CommonsChunkPlugin
  14. chunksSortMode: 'dependency' // 默认4个选项:'none', 'auto', 'dependency', '{function}'
  15. }),

7、如果在router中没有用mode: ‘history’,

  1. export function getStr(name) {
  2. // 获取url的指定属性
  3. var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
  4. var r = window.location.search.substr(1).match(reg);
  5. if (r != null) return decodeURI(r[2]);
  6. return null;
  7. }

用这个方法获得的url参数为null。
(1)此时的location是http://localhost:8081/\#/myInfoReport?storageSign=1012190735&schid=,location.search得到的是空字符串
(1)解决:router中用mode: ‘history’,后台需要配置,不然页面会显示一片空白。
6、数组更新
数组不能像对象那样自动更新,需要手动用set方法去更新

  1. // 数组更新数据
  2. this.$set(tranArr, i, tranArr[i]);

7、路由的query和params
1、this.$route.params; ,相当于post,在刷新后会为空。
2、this.$route.query; ,相当于get,在刷新后会一直存在。

8、报错:Failed to mount component:template or render function not defined. (found in )
1、原因:同目录下有同名的vue和js
2、解决:将js的文件名改成不一样的就好了。
9、js中引入vue组件
1、alert-modal.vue

  1. <div class="alert" v-if="showFlag">
  2. <div class="alert-mask"></div>
  3. <div class="alert-dialog"><div class="alert-dialog-hd">提示</div><div class="alert-dialog-bd" v-html="hintContent"></div><div class="alert-dialog-ft"><button class="alert-btn-sure" @click="alertSure">确定</button></div></div>
  4. </div>
  5. </template>
  6. <script type=text/ecmascript-6>
  7. export default {
  8. name: 'hint',
  9. props: {
  10. hintContent: {
  11. type: String,
  12. default: ''
  13. }
  14. },
  15. data() {
  16. return {
  17. showFlag: true
  18. };
  19. },
  20. methods: {
  21. alertSure() {
  22. this.showFlag = false;
  23. this.$emit('showH', false);
  24. }
  25. }
  26. };
  27. </script>
  28. <style lang="stylus" rel="stylesheet/stylus" scoped>
  29. @import "~assets/stylus/variable"
  30. .alert-mask
  31. position:fixed;
  32. left:0;
  33. right:0;
  34. top:0;
  35. bottom:0;
  36. z-index:999;
  37. background-color:rgba(0,0,0,0.6);
  38. .alert-dialog
  39. position: fixed;
  40. left:15%;
  41. right:15%;
  42. top: 30%;
  43. z-index: 1000;
  44. margin: 0;
  45. width:70%;
  46. border-radius: 3px;
  47. text-align: center;
  48. background:$color-white;
  49. overflow: hidden;
  50. .alert-dialog-hd
  51. padding: 1.2em 0 .5em;
  52. .alert-dialog-bd
  53. padding: 0 20px;
  54. font-size: $font-size-m;
  55. color: $color-text-l;
  56. word-wrap: break-word;
  57. word-break: break-all;
  58. .alert-dialog-ft
  59. margin-top: 20px;
  60. border-top:1px solid $color-border;
  61. line-height: 42px;
  62. text-align:center;
  63. .alert-btn-sure
  64. line-height:42px;
  65. font-size:$font-size-l;
  66. color: $color-theme;
  67. background:$color-white;
  68. </style>

2、同目录下创建alert-modal-component.js

  1. import Vue from 'vue';
  2. import modalModule from './alert-modal.vue';
  3. // 创建组件构造函数,类似vue的子类
  4. const ModalConstructor = Vue.extend(modalModule);
  5. const alertModal = function (options) {
  6. // 防止重复弹出
  7. let domed = document.getElementsByClassName('alert-modal');
  8. if (domed.length > 0) {
  9. return;
  10. }
  11. const {
  12. ...rest
  13. } = options;
  14. options = options || {};
  15. // 创建组件实例
  16. var instance = new ModalConstructor({
  17. propsData: {
  18. ...rest
  19. }
  20. });
  21. // 页面渲染
  22. instance.vm = instance.$mount(); // 挂载但是并未插入dom,是一个完整的Vue实例
  23. document.body.appendChild(instance.vm.$el);
  24. // instance.vm.visible = true;
  25. return instance.vm;
  26. };
  27. export default alertModal;

3、封装的axios.js中,如果error调用
import alertModal from 'base/alert/alert-modal-component.js';
alertModal({ hintContent: '弹出错误提示' });

4、以上是纯js中引入vue.
5、下面是vue直接引入另一vue组件的api,不用添加元素、import成component的形式
(1)新建index.js

  1. import AlertModal from 'base/alert/alert-modal.vue';
  2. import alertModal from 'base/alert/alert-modal-component.js';
  3. export default (Vue) => {
  4. Vue.component(AlertModal.name, AlertModal);
  5. Vue.prototype.$alertModal = alertModal;
  6. Vue.alertModal = alertModal;
  7. };

(2)main.js中引入

  1. import alertModal from 'base/alert/index.js';
  2. Vue.use(alertModal);

(3)login.vue中使用

  1. this.$alertModal({
  2. hintContent: '请输入密码'
  3. });

(4)如果在login.vue中点击弹框的确定之后要进行其他操作,则用添加元素,import成component的方法。同一页面这2种方法可以混用。
10 better-scroll无法滚动
1、自己用touchstart,touchmove写的左滑删除后,better-scroll无法滚动。
2、原因:touchmove的方法中写了e.stopPropagation();导致垂直方向无法滚动。
3、删除e.stopPropagation.
11

发表评论

表情:
评论列表 (有 0 条评论,1231人围观)

还没有评论,来说两句吧...

相关阅读

    相关 开发遇到问题

    使用spring的单元测试,依赖注入时报空指针(即依赖注入失败) 我在依赖注入上打了断点它也进不去,仔细检查了是不是启动类没有放在相应的包下面,配置类没有加@Configu