Vue -Second

1. Vue CLI中的嵌套路由

通常,在设计视图时,在App.vue中不会设计页面元素,只是添加一个<router-view/>即可!

在其它的视图中,设计的目标效果中可能存在多“页面”中相同的部分,反之,也可以理解为“某个页面的某个区域(不同的部分)是动态变化的”,则对应的区域(某个页面中会变化的部分)就可以设计为<router-view/>,同时,由于当前视图本身也是显示在App.vue设计的<router-view/>中的,就出现了“嵌套路由”!

当项目中多个视图中都使用到了<router-view/>时,某个视图组件到底显示在哪个<router-view/>,取决于路由的配置:

  • 如果某个视图的路由配置在src/router/index.jsroutes常量中,则此视图将显示在App.vue<router-view/>

    const routes = [
      {
        path: '/',
        component: HomeView
      },
      // 以下AboutView将显示在App.vue的<router-view/>中
      {
         path: '/about',
         component: () => import('../views/AboutView.vue')
       }
    ];
    
  • 如果某个视图的路由配置在src/router/index.jsroutes常量中的某个路由配置的子级,则此视图将显示在其父级路由的视图中

    const routes = [
      {
        path: '/',
        component: HomeView,
        // 以下AboutView将显示在HomeView的<router-view/>中
        children: [
          {
            path: '/about',
            component: () => import('../views/AdminIndexView.vue')
          }
        ]
      }
    ];
    

2. Vue CLI项目的启动端口

Vue CLI项目在启动时,默认将尝试占用8080端口,如果此端口已经被占用,则会顺延后一位端口号,即尝试占用8081……当然,如果8081也被占用,则会继续顺延至8082,以此类推。

建议为Vue CLI项目显式的指定端口,避免发生冲突,或多次启动时的端口号不一致。需要在package.json,原本有(通常在第6行):

"serve": "vue-cli-service serve",

在以上属性值的末尾添加--port 端口号,例如:

"serve": "vue-cli-service serve --port 8888",

3. 在Vue CLI项目中使用axios

首先,需要安装axios:

npm i axios -S

然后,需要在main.js中导入,并声明为Vue对象的成员:

import axios from 'axios';

Vue.prototype.axios = axios;

4. 关于跨域访问

默认情况下,不允许执行跨域访问(从某一台服务器向另一台服务器发起异步请求),如果出现跨域访问,在浏览器的错误提示大致如下:

Access to XMLHttpRequest at 'http://localhost:8080/login' from origin 'http://localhost:9000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

提示:以上错误的关键字是:CORS

在基于Spring Boot开发的服务器端项目中,添加一个Spring MVC的配置类即可允许跨域访问!

可以让Spring Boot项目的启动类实现WebMvcConfigurer接口,并在此类中添加以下代码:

@Override
public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/**")
            .allowedOriginPatterns("*")
            .allowedHeaders("*")
            .allowedMethods("*")
            .allowCredentials(true)
            .maxAge(3600);
}

完成后,重启服务器端项目,然后,使用客户端再次发出请求,此请求可以正常发出(浏览器不会报告错误),且服务器端可以接收到请求参数。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
These dependencies were not found: * @/api/second/category/industry in ./node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/babel-loader/lib!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/views/trivoltine/std_base/editStructure.vue?vue&type=script&lang=js& * @/api/second/structure/crud in ./node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/babel-loader/lib!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/views/trivoltine/std_base/seeStructure.vue?vue&type=script&lang=js& * @/components/tinymce-editor/tinymce-editor.vue in ./node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/babel-loader/lib!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/views/trivoltine/std_base/editStructure.vue?vue&type=script&lang=js& * vue-pdf in ./node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/babel-loader/lib!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/views/trivoltine/std_base/editStructure.vue?vue&type=script&lang=js& * vue-quill-editor in ./node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/babel-loader/lib!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/views/trivoltine/std_base/editStructure.vue?vue&type=script&lang=js& To install them, you can run: npm install --save @/api/second/category/industry @/api/second/structure/crud @/components/tinymce-editor/tinymce-editor.vue vue-pdf vue-quill-editor
05-23

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值