vue项目问题总结

路由router(params和query的区别)

相同点:
1、使用方式相同

params:this.$router.push({
    
    name: 'video', params: params})
query:  this.$router.push({
    
    name: 'video', query: params})

不同点:
1、params是路由的一部分,必须要有。query是拼接在url后面的参数,没有也没关系。

2、params、query不设置也可以传参,但是params不设置的时候,刷新页面或者返回参数会丢失,query并不会出现这种情况。

3、地址传参格式
params 获取参数的路由配置为:‘/count/:id ’ 例:’/count/123 ’
query获取参数的路由配置为:’/count?key=value’ 例:’/count?id=123 ’

params一旦设置在路由,params就是路由的一部分,如果这个路由有params传参,但是在跳转的时候没有传这个参数,会导致跳转失败或者页面会没有内容。

刷新丢失数据的问题:
如果路由上面不写参数,也是可以传过去的,但不会在url上面显示出你的参数,并且当你跳到别的页面或者刷新页面的时候参数会丢失(如下图所示),那依赖这个参数的http请求或者其他操作就会失败。


添加全局对象

Vue.prototype: 添加 Vue 实例对象(方法或者常量等)
Vue.use() :添加插件 (自动阻止多次注册相同插件,届时即使多次调用也只会注册一次该插件。)

两者在main.js中直接使用。


Axios 基于Promise 的http库

实例:

axios({
    
    })
请求配置字段说明:
{
    
    
  // `url` 是用于请求的服务器 URL
  url: '/user',

  // `method` 是创建请求时使用的方法
  method: 'get', // 默认是 get

  // `baseURL` 将自动加在 `url` 前面,除非 `url` 是一个绝对 URL。
  // 它可以通过设置一个 `baseURL` 便于为 axios 实例的方法传递相对 URL
  baseURL: 'https://some-domain.com/api/',

  // `transformRequest` 允许在向服务器发送前,修改请求数据
  // 只能用在 'PUT', 'POST' 和 'PATCH' 这几个请求方法
  // 后面数组中的函数必须返回一个字符串,或 ArrayBuffer,或 Stream
  transformRequest: [function (data) {
    
    
    // 对 data 进行任意转换处理

    return data;
  }],

  // `transformResponse` 在传递给 then/catch 前,允许修改响应数据
  transformResponse: [function (data) {
    
    
    // 对 data 进行任意转换处理

    return data;
  }],

  // `headers` 是即将被发送的自定义请求头
  headers: {
    
    'X-Requested-With': 'XMLHttpRequest'},

  // `params` 是即将与请求一起发送的 URL 参数
  // 必须是一个无格式对象(plain object)或 URLSearchParams 对象
  params: {
    
    
    ID: 12345
  },

  // `paramsSerializer` 是一个负责 `params` 序列化的函数
  // (e.g. https://www.npmjs.com/package/qs, http://api.jquery.com/jquery.param/)
  paramsSerializer: function(params) {
    
    
    return Qs.stringify(params, {
    
    arrayFormat: 'brackets'})
  },

  // `data` 是作为请求主体被发送的数据
  // 只适用于这些请求方法 'PUT', 'POST', 和 'PATCH'
  // 在没有设置 `transformRequest` 时,必须是以下类型之一:
  // - string, plain object, ArrayBuffer, ArrayBufferView, URLSearchParams
  // - 浏览器专属:FormData, File, Blob
  // - Node 专属: Stream
  data: {
    
    
    firstName: 'Fred'
  },

  // `timeout` 指定请求超时的毫秒数(0 表示无超时时间)
  // 如果请求话费了超过 `timeout` 的时间,请求将被中断
  timeout: 1000,

  // `withCredentials` 表示跨域请求时是否需要使用凭证
  withCredentials: false, // 默认的

  // `adapter` 允许自定义处理请求,以使测试更轻松
  // 返回一个 promise 并应用一个有效的响应 (查阅 [response docs](#response-api)).
  adapter: function (config) {
    
    
    /* ... */
  },

  // `auth` 表示应该使用 HTTP 基础验证,并提供凭据
  // 这将设置一个 `Authorization` 头,覆写掉现有的任意使用 `headers` 设置的自定义 `Authorization`头
  auth: {
    
    
    username: 'janedoe',
    password: 's00pers3cret'
  },

  // `responseType` 表示服务器响应的数据类型,可以是 'arraybuffer', 'blob', 'document', 'json', 'text', 'stream'
  responseType: 'json', // 默认的

  // `xsrfCookieName` 是用作 xsrf token 的值的cookie的名称
  xsrfCookieName: 'XSRF-TOKEN', // default

  // `xsrfHeaderName` 是承载 xsrf token 的值的 HTTP 头的名称
  xsrfHeaderName: 'X-XSRF-TOKEN', // 默认的

  // `onUploadProgress` 允许为上传处理进度事件
  onUploadProgress: function (progressEvent) {
    
    
    // 对原生进度事件的处理
  },

  // `onDownloadProgress` 允许为下载处理进度事件
  onDownloadProgress: function (progressEvent) {
    
    
    // 对原生进度事件的处理
  },

  // `maxContentLength` 定义允许的响应内容的最大尺寸
  maxContentLength: 2000,

  // `validateStatus` 定义对于给定的HTTP 响应状态码是 resolve 或 reject  promise 。如果 `validateStatus` 返回 `true` (或者设置为 `null` 或 `undefined`),promise 将被 resolve; 否则,promise 将被 rejecte
  validateStatus: function (status) {
    
    
    return status >= 200 && status < 300; // 默认的
  },

  // `maxRedirects` 定义在 node.js 中 follow 的最大重定向数目
  // 如果设置为0,将不会 follow 任何重定向
  maxRedirects: 5, // 默认的

  // `httpAgent` 和 `httpsAgent` 分别在 node.js 中用于定义在执行 http 和 https 时使用的自定义代理。允许像这样配置选项:
  // `keepAlive` 默认没有启用
  httpAgent: new http.Agent({
    
     keepAlive: true }),
  httpsAgent: new https.Agent({
    
     keepAlive: true }),

  // 'proxy' 定义代理服务器的主机名称和端口
  // `auth` 表示 HTTP 基础验证应当用于连接代理,并提供凭据
  // 这将会设置一个 `Proxy-Authorization` 头,覆写掉已有的通过使用 `header` 设置的自定义 `Proxy-Authorization` 头。
  proxy: {
    
    
    host: '127.0.0.1',
    port: 9000,
    auth: : {
    
    
      username: 'mikeymike',
      password: 'rapunz3l'
    }
  },

  // `cancelToken` 指定用于取消请求的 cancel token
  // (查看后面的 Cancellation 这节了解更多)
  cancelToken: new CancelToken(function (cancel) {
    
    
  })
}


相应配置说明:

{
    
    
  // `data` 由服务器提供的响应
  data: {
    
    },

  // `status` 来自服务器响应的 HTTP 状态码
  status: 200,

  // `statusText` 来自服务器响应的 HTTP 状态信息
  statusText: 'OK',

  // `headers` 服务器响应的头
  headers: {
    
    },

  // `config` 是为请求提供的配置信息
  config: {
    
    }
}

报错

Refused to load the image ‘http://localhost:8080/favicon.ico’
because it violates the following Content Security Policy directive:
“default-src ‘none’”. Note that ‘img-src’ was not explicitly set,
so ‘default-src’ is used as a fallback.
解析:运行环境路径问题导致
解决办法:config(文件夹) => index.js(文件) dev(也就是运行编译的配置项数组)=> assetsPublicPath这个路径为 “/”


VUE 设置全局变量

、外部文件定义一个 .vue模块或者直接在main里面赋值变量
如果外部文件形式,变量用export 暴露出去,mainjs则不需要,因为不用引入文件
使用:文件形式需要引入 import global_ from ‘components/tool/Global’,此模块放入文件或者main都可以使用
main文件直接赋值变量形式 则不需要引入 赋值于Vue.prototype ,下次使用的时候直接this就能获取

二、vuex 状态管理模式
适用于大型存储。状态存储是响应式的


vue 设置动态标题

1、通过访问当前路由对象($route)的 meta 属性即可获取到 title 值。

const routes = [
  {
    
    
    path: '/',
    ...
    meta: {
    
    
      title: '首页'
    }
  }
]

2、路由设置动态标题(放在mainJs即可)

router.beforeEach((to, from, next) => {
    
    
  document.title = to.meta.title || '默认title'
  next()
})

3、页面中(刷新之后会丢失title)

beforeCreate () {
    
    
    console.log(this.$route.meta)
    document.title = this.$route.meta.title || '默认title'
  },

4、index.html 入口页title清空
因为刷新页面的时候会闪烁,影响体验


vue 列表数据缓存

参考文档:https://blog.csdn.net/muguli2008/article/details/103687284

Vue 详情页返回列表页,不刷新页面,并且保留列表页原来滚动条所在位置

1、路由配置添加meta属性 keepAlive: true //开启缓存

routes: [
    {
    
    
      path: '/',
      name: 'News',
      component: () => import('../views/News.vue'),
      meta: {
    
    
        keepAlive: true    //开启缓存
      }
    },
]

2、在App.vue文件中,包裹

<template>
  <div id="app">
    <keep-alive>
          <!-- 如果当前打开页面的路由中 keepAlive: true (开启了缓存时) -->
          <router-view v-if="$route.meta.keepAlive"></router-view>
    </keep-alive>
        
    <!-- 如果当前打开页面的路由中 没有 或者为 keepAlive: false (关闭缓存时[默认就是false]) -->
    <router-view v-if="!$route.meta.keepAlive"></router-view>
  </div>
</template>

3、监听滚动条并滚动到相应位置(生命周期函数):

// 离开路由之前执行的函数
 
  beforeRouteLeave(to, from, next) {
    
    
 
    // 如果在window中出现的滚动条
    // this.scroll = window.scrollHeight;
 
    //  如果在某个指的元素中出现的滚动条 就在该素中添加ref属性,如:ref="listBox"
    this.scroll = this.$refs.listBox.scrollHeight;
    
    next()
  },



// 进入路由之前执行的函数
 
  beforeRouteEnter(to, from, next) {
    
    
    next(vm => {
    
    
 
      // 如果在window中出现的滚动条
      // window.scrollTop = vm.scroll;
 
      // 如果在某个指的元素中出现的滚动条 就在该素中添加ref属性,如:ref="listBox"
      vm.$refs.listBox.scrollTop = vm.scroll
    })
  }

如何禁止 iPhone Safari 视频自动全屏?

1、前端将video标签加入属性 webkit-playsinline,如:

<video id="player" width="480" height="320" webkit-playsinline>

2、Obj-C中,添加配置

webview.allowsInlineMediaPlayback = YES;

移动端HTML5,ios(针对safari浏览器)点击事件闪现黑灰色背景解决方案

*{
    
    
    -webkit-tap-highlight-color: transparent;   //设置为透明
 }

猜你喜欢

转载自blog.csdn.net/qq_27751965/article/details/109363097