一.路由跳转

  1.1 项目的初始化

  vue create m-proj   >>>创建vue项目

  精简vue项目的

  views 视图   About(基本是删除的) Home.(可以作为主业去设置 搭建一些基本的样式)

  src 下创建一个css 作为全局的样式 设置

  最后将设置好的全局样式 在mian.js 文件中 导入 作为 项目的入口 类似执行文件(start.py)

  基础的完成了

  

  二.路由传参的几种方法

    2.1 导航栏的显示设置

    

  1. <template>
    <div class="nav">
    <ul><li :class="{active: currentPage==='/'}">
    <!--<li @click="changePage('/')" :class="{active: currentPage==='/'}">-->
    <!--<a href="/" >主页</a>-->
    <router-link to="/">主页</router-link>
  2.  
  3. </li>
  4.  
  5. <li :class="{active: currentPage==='/course'}">
    <!--<li @click="changePage('/')" :class="{active: currentPage==='/'}">-->
    <!--<a href="/" >主页</a>-->
    <router-link to="/Course">课程</router-link>
  6.  
  7. </li>
    </ul>
    </div>
    </template>
  8.  
  9. <script>
    export default {
    name: "Nav",
    // changePage()加括号表示有数据需要传参
  10.  
  11. data(){
    return{
    // 需要将数据的返回
    // 每渲染一个页面,都会出现加载Nav组件,currentPage就会被重置,
    // 1)在点击跳转事件中,将跳转的页面用 数据库 保存,在钩子函数中对currentPage进行数据更新
    currentPage:'',
    }
  12.  
  13. },
    // 方法:
    // methods: {
    // // 点击事件
    // changePage(page){
    // // 进行路由的跳转
    // this.$router.push(page);
    //
    //
    //
    // }
    //
    // },
    // 当前组件加载成功,要根据当前实际所在的路径,判断单选激活标签
    cerate(){
    // 获取路径 导航栏每被加载一次 被点击的页面 就被显示高量 : 也就是显示
    this.currentPage=this.$route.path;
  14.  
  15. }
    }
    </script>
  16.  
  17. <style scoped>
    .nav {
    width: 100%;
    height: 60px;
    background-color: dodgerblue;
    }
  18.  
  19. .nav li {
    float: left;
    padding: 15px 30px;
    font: normal 30px/30px '微软雅黑';
  20.  
  21. }
    /*.nav li:hover {*/
    /*cursor: pointer;*/
    /**/
  22.  
  23. /*}*/
    /*.nav li.active {*/
    /*cursor: pointer;*/
    /**/
    /*}*/
    /*!*这里的设置主要为了 我们能搞在导航栏进行 点击一整块 扩大范围*!*/
    /*.nav li a {*/
    /*display: block;*/
    /*height: 30px;*/
    /*}*/
    .nav li:hover {
    cursor: pointer;
    background-color: aquamarine;
    }
    .nav li.active {
    cursor: pointer;
    background-color: aquamarine;
    }
  24.  
  25. </style>
  26.  
  27. 将导航栏组件() 在 view 中注册
  1. // 将导航栏进行 导入和注册 上面写Nav 标签
  2.  
  3. import Nav from '@/components/Nav.vue'
    export default {
    name: 'home',
    components: {
    Nav,
  4.  
  5. },
  1. <div class="home">
    <Nav></Nav> 引入组件
    </div>

2.2 路由进行跳转的四大方法

  

  1. <template>
    <div class="home">
    <Nav></Nav>
  2.  
  3. <!--// 自己设计一个页面跳转-->
    <div class="router">
    <!--设置路由 设计点击按钮-->
    <!--方法 1 path --> 方法
    <button type="button" @click="goPage('/course')">课程页</button>
    <button type="button" @click="goPage('/')">主页</button>
  4.  
  5. <!--// 方法 2 go(数字)-->方法
    <button type="button" @click="goBack">返回上一页</button>
  6.  
  7. <!--// 方法3 name -->方法
  8.  
  9. <!-- 拓展性-->
    <button type="button" @click="goPageName('course')">通过name进行跳转</button>
  10.  
  11. <!--// 方法(4) 将 name router 中的应用--> 运用a标签的特性
    <!--//原来的条状 to ='' a 标签的属性-->
    <!--<router-link to="/course">课程页的name </router-link>>-->
    <!--// 换成变量 {} :to属性的要特别注意 'course' 是取值所以要字符串-->
    <router-link :to="{name:'course'}">课程页的name </router-link>>
  12.  
  13. </div>
    <h1>主页</h1>
  14.  
  15. </div>
    </template>
  16.  
  17. <script>
    // 将导航栏进行 导入和注册 上面写Nav 标签
  18.  
  19. import Nav from '@/components/Nav.vue'
    export default {
    name: 'home',
    components: {
    Nav,
  20.  
  21. },
  22.  
  23. methods: {
    goPage(page) {
    // 逻辑跳转 (1)
    let currentPage = this.$route.path; // router.path 取值
    // 若是home 页面就不让他进行跳转
    if (currentPage !==page){
    this.$router.push(page)
    }
  24.  
  25. },
  26.  
  27. // (2)
    goBack(){
    // ji逻辑使用history 返回上一层
    // this.$router.go(-1)
    // 返回上量页
    // this.$router.go(-2);
  28.  
  29. // 前进一页
    // this.$router.go(1);
    // 前进页
    // this.$router.go(2);
  30.  
  31. },
    // (3)
    // 总结路由跳转的方式 点击事件
    goPageName(goPageName){
    this.$router.push(goPageName)
    }
  32.  
  33. }
  34.  
  35. }
    </script>

  2.2 三种传参方式

  1. <template>
    <div class="course-card">
    <!--// {{// 变量相关}}-->
    <h1 @click="goDetail">{{course.name}}</h1>
        
  1. <!--方法二:link 直接跳转-->
    <router-link :to="`course/${course.id}/detail`">{{course.name}}</router-link>
    </div>
  1. </div>
    </template>
  2.  
  3. <script>
  4.  
  5. export default {
    name: "CourseCard",
    // 从父标签
    // <div class="main">
    // <CourseCard v-for="course in course_list" :key="course.name" :course="course"></CourseCard>
    // </div>
    props:['course'],
          方法
  1. methods:{
    // goDetail(){
    // this.$router.push({
    // // 跳转到详情页 进行页面详情展示
    // name:'course-detail',
    // 点击的同时发送id 详情页可以通过param query 获取
  2.  
  3. //Object fullPath: "/course/detatil"
    // hash: ""
    // matched: [{…}]
    // meta: {}
    // name: "course-detail"
    // params: {}
    // path: "/course/detatil"
    // query: {}
    // __proto__: Object
    // 这是可以 详情页可以进行接收的参数
  4.  
  5. // 第一种传参方式
    // params: {id:this.course.id},
    // 第一种传参过去 跳转后页面会刷新
  6.  
  7. // 第二种传参方式
    // query: {id:this.course.id}
    // 第一种传参过去 跳转后页面不会刷新
  8.  
  9. // 第三种常用比较灵活()
    goDetail(){
    // 跳转到详情页 进行页面详情展显示 >>> 手动拼接不再通过name
    // params: {id: "2"} d第三种的话是将 $是前端的站位符号 %s
    this.$router.push(`/course/${this.course.id}/detail`)
    }
    }
  10.  
  11. }
  12.  
  13. </script>
  14.  
  15. <style scoped>
    /*// 设置一下 我们课程列表的样式*/
    .course-card h1{
    width: 250px;
    height: 250px;
    background-color:lightsalmon;
    border-radius: 50%;
    font:normal 22px/250px 'STSong';
    float: left;
    text-align: center;
    cursor: pointer;
    margin: 20px;
  16.  
  17. }
  18.  
  19. </style>

  三.vue-cookies 插件的 下载-配置-使用

  

D:\MY-vue\my-vueprj2>cnpm install vue-cookies

万一安装错了

D:\MY-vue\my-vueprj2>cnpm uninstall vue-cookies

再次安装cnpm install vue-cookies

配置

  1. 这里出错了 ???? 回去整理

  四.axios -- vue 中的ajax 异步提价数据

  安装

D:\MY-vue\my-vueprj2>cnpm install axios

 

  

  配置

  main,js

  1. import Vue from 'vue'
  2. import App from './App.vue'
  3. import router from './router'
  4. import store from './store'
  5.  
  6. Vue.config.productionTip = false;
  7.  
  8. // 项目的初始化全局样式的配置
  9. import '@/assets/css/global.css'
  10.  
  11. // 配置全局的cookies
  12. import cookies from 'vue-cookies'
  13. // 直接舍给vue原型
  14. Vue.prototype.$cookies=cookies;
  15.  
  16. // 配置axios 请求后台的ajax
  17. import axios from 'axios'
  18. Vue.prototype.$axios=axios;
  19. new Vue({
  20. router,
  21. store,
  22.  
  23. render: h => h(App)
  24. }).$mount('#app');

  使用

  1. <template>
  2.  
  3. <div class="test-page">
  4. <Nav></Nav>
  5. <h1>测试页面</h1>
  6. <p>
  7. <input type="text" v-model=" tokenInput">
  8. <button @click="getToken">获取token</button>
  9.  
  10. </p>
  11. <hr>
  12. <div class="ajax">
  13. <input type="text" v-model="username">
  14. <button @click="ajaxAction">提交按钮</button>
  15.  
  16. </div>
  17.  
  18. </div>
  19. </template>
  20.  
  21. <script>
  22. import Nav from '@/components/Nav.vue'
  23. export default {
  24. name: "TestPage",
  25. components:{
  26. Nav
  27. },
  28. data(){
  29. return{
  30. tokenInput:'',
  31. token:'',
  32. username:'',
  33. }
  34. },
  35. // 获取token
  36. methods:{
  37. getToken(){
  38. // 什么是token
  39. // 谁产生的 后台的存储(session表,文件,缓存) 前台存储cookie
  40. // 怎么用 服务器生成反给 前台 登录认证 登录后的请求
  41. // if (this.token){
  42. // let token=this. tokenInput ;
  43. // // token获取后需要前台自己进行存储cookie
  44. // // 增 查 更改 删
  45. // // 安装vue-cookies
  46. // }
  47.  
  48. },
  49.  
  50. // ajax
  51. ajaxAction(){
  52. if (this.username){
  53. //
  54. this.$axios({
  55. url:"http://127.0.0.1:8000/test/ajax",
  56. methods:'get',
  57. // 请求发送数据 下面的参数
  58.  
  59. params:{
  60. username:this.username,
  61. }
  62. // 回调函数
  63. // 成功是
  64. }).then(function (response) {
  65. console.log(response)
  66. // 失败是 打印
  67. }).catch(function (error) {
  68. console.log(error)
  69. });
  70. data 也可以发数据 方法二
  71.  
  72. this.$axios({
  73. url:"http://127.0.0.1:8000/test/ajax",
  74. methods:'post',
  75. // 请求发送数据 下面的参数
  76.  
  77. data:{
  78. username:this.username,
  79. }
  80. // 回调函数
  81. // 成功是
  82. }).then(function (response) {
  83. console.log(response)
  84. // 失败是 打印
  85. }).catch(function (error) {
  86. console.log(error)
  87. })
  88. }
  89.  
  90. }
  91.  
  92. }
  93.  
  94. }
  95. </script>
  96.  
  97. <style scoped>
  98.  
  99. </style>

  

    下载-安装-使用

    后端提交数据 -=--cookio(跨域问题)同源策略

    不是同一服务器发来的请求 拒绝请求 同源策略(CORS)

    如何解决

    django解决跨域问题

    后台安装django-cors-headers 模块

    

   (1) D:\day67_djproj>pip install django-cors-headers)

   (2))注册

  

  1. INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app01.apps.App01Config',
    'corsheaders'
    ]

   (3) 设置中间件

  1. MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    # 'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'corsheaders.middleware.CorsMiddleware'
    ]

      (4)设置跨域

  

  1. # 设置苦于问题 允许跨域
    CORS_ORIGIN_ALLOW_ALL = True

    上线指定 传输的路径

  五.element-ui

   下载 一定要会用多练啊 在研究一下 老师的视屏

   还有就是cookies 的增改 查 删

 

D:\day67_djproj>npm i element-ui -S

   -配置-

main.js

import ElementUI from 'element-ui';

import 'element-ui/lib/theme-chalk/index.css';

Vue.use(ElementUI);

  使用

文档

  1. ## 路由传参
  2.  
  3. ### 第一种
  4.  
  5. ##### router.js
  6.  
  7. ```js
  8. routes: [
  9. // ...
  10. {
  11. path: '/course/:id/detail',
  12. name: 'course-detail',
  13. component: CourseDetail
  14. },
  15. ]
  16. ```
  17.  
  18. ##### 跳转.vue
  19.  
  20. ```vue
  21. <template>
  22. <router-link :to="`/course/${course.id}/detail`">{{ course.name }}</router-link>
  23. </template>
  24. <script>
  25. // ...
  26. goDetail() {
  27. this.$router.push(`/course/${this.course.id}/detail`);
  28. }
  29. </script>
  30. ```
  31.  
  32. ##### 接收.vue
  33.  
  34. ```js
  35. created() {
  36. let id = this.$route.params.id;
  37. }
  38. ```
  39.  
  40. ### 第二种
  41.  
  42. ##### router.js
  43.  
  44. ```js
  45. routes: [
  46. // ...
  47. {
  48. path: '/course/detail',
  49. name: 'course-detail',
  50. component: CourseDetail
  51. },
  52. ]
  53. ```
  54.  
  55. ##### 跳转.vue
  56.  
  57. ```vue
  58. <template>
  59. <router-link :to="{
  60. name: 'course-detail',
  61. query: {id: course.id}
  62. }">{{ course.name }}</router-link>
  63. </template>
  64. <script>
  65. // ...
  66. goDetail() {
  67. this.$router.push({
  68. name: 'course-detail',
  69. query: {
  70. id: this.course.id
  71. }
  72. });
  73. }
  74. </script>
  75. ```
  76.  
  77. ##### 接收.vue
  78.  
  79. ```js
  80. created() {
  81. let id = this.$route.query.id;
  82. }
  83. ```
  84.  
  85. ```
  86. export default new Vuex.Store({
  87. state: {
  88. title: '默认值'
  89. },
  90. mutations: {
  91. // mutations 为 state 中的属性提供setter方法
  92. // setter方法名随意,但是参数列表固定两个:state, newValue
  93. setTitle(state, newValue) {
  94. state.title = newValue;
  95. }
  96. },
  97. actions: {}
  98. })
  99.  
  100. 赋值
  101. this.$store.state.title = 'newTitle'
  102. this.$store.commit('setTitle', 'newTitle')
  103.  
  104. 取值
  105. console.log(this.$store.state.title)
  106. ```
  107.  
  108. ## vue-cookie插件
  109.  
  110. 安装
  111.  
  112. ```
  113. >: cnpm install vue-cookies
  114. ```
  115.  
  116. main.js 配置
  117.  
  118. ```js
  119. // 第一种
  120. import cookies from 'vue-cookies' // 导入插件
  121. Vue.use(cookies); // 加载插件
  122. new Vue({
  123. // ...
  124. cookies, // 配置使用插件原型 $cookies
  125. }).$mount('#app');
  126.  
  127. // 第二种
  128. import cookies from 'vue-cookies' // 导入插件
  129. Vue.prototype.$cookies = cookies; // 直接配置插件原型 $cookies
  130. ```
  131.  
  132. 使用
  133.  
  134. ```js
  135. // 增(改): key,value,exp(过期时间)
  136. // 1 = '1s' | '1m' | '1h' | '1d'
  137. this.$cookies.set('token', token, '1y');
  138.  
  139. // 查:key
  140. this.token = this.$cookies.get('token');
  141.  
  142. // 删:key
  143. this.$cookies.remove('token');
  144. ```
  145.  
  146. 注:cookie一般都是用来存储token
  147.  
  148. ```js
  149. // 1) 什么是token:安全认证的字符串
  150. // 2) 谁产生的:后台产生
  151. // 3) 谁来存储:后台存储(session表、文件、内存缓存),前台存储(cookie)
  152. // 4) 如何使用:服务器先生成反馈给前台(登陆认证过程),前台提交给后台完成认证(需要登录后的请求)
  153. // 5) 前后台分离项目:后台生成token,返回给前台 => 前台自己存储,发送携带token请求 => 后台完成token校验 => 后台得到登陆用户
  154. ```
  155.  
  156. ## axios插件
  157.  
  158. 安装
  159.  
  160. ```
  161. >: cnpm install axios
  162. ```
  163.  
  164. main.js配置
  165.  
  166. ```js
  167. import axios from 'axios' // 导入插件
  168. Vue.prototype.$axios = axios; // 直接配置插件原型 $axios
  169. ```
  170.  
  171. 使用
  172.  
  173. ```js
  174. this.axios({
  175. url: '请求接口',
  176. method: 'get|post请求',
  177. data: {post等提交的数据},
  178. params: {get提交的数据}
  179. }).then(请求成功的回调函数).catch(请求失败的回调函数)
  180. ```
  181.  
  182. 跨域问题(同源策略)
  183.  
  184. ```js
  185. // 后台接收到前台的请求,可以接收前台数据与请求信息,发现请求的信息不是自身服务器发来的请求,拒绝响应数据,这种情况称之为 - 跨域问题(同源策略 CORS)
  186.  
  187. // 导致跨域情况有三种
  188. // 1) 端口不一致
  189. // 2) IP不一致
  190. // 3) 协议不一致
  191.  
  192. // Django如何解决 - django-cors-headers模块
  193. // 1) 安装:pip3 install django-cors-headers
  194. // 2) 注册:
  195. INSTALLED_APPS = [
  196. ...
  197. 'corsheaders'
  198. ]
  199. // 3) 设置中间件:
  200. MIDDLEWARE = [
  201. ...
  202. 'corsheaders.middleware.CorsMiddleware'
  203. ]
  204. // 4) 设置跨域:
  205. CORS_ORIGIN_ALLOW_ALL = True
  206. ```
  207.  
  208. ## element-ui插件
  209.  
  210. 安装
  211.  
  212. ```
  213. >: cnpm i element-ui -S
  214. ```
  215.  
  216. main.js配置
  217.  
  218. ```js
  219. import ElementUI from 'element-ui';
  220. import 'element-ui/lib/theme-chalk/index.css';
  221. Vue.use(ElementUI);
  222. ```
  223.  
  224. 使用
  225.  
  226. ```
  227. 依照官网 https://element.eleme.cn/#/zh-CN/component/installation api
  228. ```

Vue之路由跳转 传参 aixos 和cookie的更多相关文章

  1. vue路由(一个包含重定向、嵌套路由、懒加载的main.js如下)and 路由跳转传参的query和params的异同

    import Vue from 'vue'import VueRouter from 'vue-router'import App from './App'Vue.use(VueRouter)cons ...

  2. Vue Router路由导航及传参方式

    路由导航及传参方式 一.两种导航方式 ①:声明式导航 <router-link :to="..."> ②:编程式导航 router.push(...) 二.编程式导航参 ...

  3. Vue ---- 组件文件分析 组件生命周期钩子 路由 跳转 传参

    目录 Vue组件文件微微细剖 Vue组件生命周期钩子 Vue路由 1.touter下的index.js 2.路由重定向 3.路由传参数 补充:全局样式导入 路由跳转 1. router-view标签 ...

  4. vue路由跳转传参的两种方法

    路由跳转: this.$router.push({ name: '工单列表', params: {p_camera_dev_name: 'xxx'} }); 使二级菜单呈点击状态: $('[index ...

  5. vue 路由跳转传参

    <li v-for="article in articles" @click="getDescribe(article.id)"> getDescr ...

  6. vue 中 this.$router.push() 路由跳转传参 及 参数接收的方法

    传递参数的方法:1.Params 由于动态路由也是传递params的,所以在 this.$router.push() 方法中 path不能和params一起使用,否则params将无效.需要用name ...

  7. vue路由跳转传参

    this.$router.push({ path: '/message/result_notice', query: { id: id } }) // let type = this.$route.n ...

  8. angularjs 中通过 $location 进行路由跳转传参

    $location.path('/page1').search({id: $scope.id,name:$scope.name}); 带参数跳转页面,在新的页面通过$routeParams接收参数 $ ...

  9. vue 利用路由跨页传参

    第一页,点击进入第二页进行传值: <template> <div id="app"> <div><router-link to=" ...

随机推荐

  1. Oracle --45 个非常有用的 Oracle 查询语句

    日期/时间 相关查询 1.获取当前月份的第一天运行这个命令能快速返回当前月份的第一天.你可以用任何的日期值替换 “SYSDATE”来指定查询的日期.SELECT TRUNC (SYSDATE, 'MO ...

  2. [IOI2008/BZOJ1791 岛屿](处理基环树的小技巧&基于bfs树形DP)

    IOI2008/BZOJ1791 岛屿 题目大意是在一个基环树森林里求每一棵基环树的直径①的和. 其实就是树的直径的基环树升级版.我们先把环找出来,然后从环上的每一个节点x出发,并且不经过环上其他节点 ...

  3. [CSP-S模拟测试]:最小距离(最短路)

    题目传送门(内部题97) 输入格式 第一行三个整数$n,m,p$,第二行$p$个整数$x_1\sim x_p$表示特殊点的编号.接下来$m$行每行三个整数$u,v,w$表示一条连接$u$和$v$,长度 ...

  4. 前台ajax传数组,后台java接收

    后端 //添加 @RequestMapping(value = "checkChoise") @ResponseBody ResultJson checkChoise(@Reque ...

  5. CentOS 6.5系统使用yum方式安装LAMP环境和phpMyAdmin,mysql8.0.1/mysql5.7.22+centos7,windows mysql安装、配置

    介绍如何在CentOs6.2下面使用YUM配置安装LAMP环境,一些兄弟也很喜欢使用编译的安装方法,个人觉得如果不是对服务器做定制,用yum安装稳定简单,何必去download&make&am ...

  6. mysql命令使用3

    算术运算函数 sum()求和 mysql> select sum(price) from books;+------------+| sum(price) |+------------+| 10 ...

  7. 1443:【例题4】Addition Chains

    1443:[例题4]Addition Chains 题解 注释在代码里 注意优化搜索顺序以及最优化剪枝 代码 #include<iostream> #include<cstdio&g ...

  8. nginx请求转发配置

    以下为无ssl证书配置的请求转发 server { listen ; server_name api.****.com; #以下为指定请求域名匹配到某一个端口 #location ~* /union ...

  9. java hashset输出

    for (Map.Entry<String, String> me : id_label_map.entrySet()) { System.out.println(me.getKey() ...

  10. PostgreSQL 实现按月按年,按日统计 分组统计

    endtime 是 timestamp select to_char(endtime, 'YYYY-MM-DD') as d , count(objectid) FROM sde.polygon wh ...