课堂笔记十月十一

复习

"""
一、vue项目环境:
    node => npm(cnpm) => vue/cli
二、vue项目建立:
    vue create 项目
    在pycharm中配置npm项目启动
三、项目目录
四、main.js:程序的入口文件
    加载vue环境
    加载插件环境:路由、仓库、ajax、cookie、element-ui...
    加载自定义环境:全局样式(global.css)、全局配置(settings.js)
    渲染根组件
五、.vue文件形式组件:
    template标签:内部有且只有一个根标签
    script标签:export default {},导出该局部组件内容
    style标签:scope属性,实现样式的组件化
六、项目运行的生命周期:main.js => router.js => 连接 => 页面组件 => 替换根组件中的 router-view 标签完成页面渲染 => 经过 router-link | this.$router.push() 切换路由(连接) => 完成渲染组件的替换 => 页面的跳转
七、新建页面的三步骤:
    1)建立页面组件
    2)设置组件路由
    3)设置路由跳转
八、组件的生命周期钩子:组件从生成到销毁整个过程的一些特殊时间节点回调的函数
九、this.$router路由跳转 | this.$route路由数据 (this.$route.path)
"""

路由跳转

this.$router.push('/course');
this.$router.push({name: course});
this.$router.go(-1);
this.$router.go(1);
<router-link to="/course">课程页</router-link>
<router-link :to="{name: 'course'}">课程页</router-link>

路由传参

第一种

router.js
routes: [
    // ...
    {
        path: '/course/:id/detail',
        name: 'course-detail',
        component: CourseDetail
    },
]
跳转.vue
<template>
    <router-link :to="`/course/${course.id}/detail`">{{ course.name }}</router-link>
</template>
<script>
    // ...
    goDetail() {
        this.$router.push(`/course/${this.course.id}/detail`);
    }
</script>
接收.vue
created() {
    let id = this.$route.params.id;
}

第二种

router.js
routes: [
    // ...
    {
        path: '/course/detail',
        name: 'course-detail',
        component: CourseDetail
    },
]
跳转.vue
<template>
    <router-link :to="{
            name: 'course-detail',
            query: {id: course.id}
        }">{{ course.name }}</router-link>
</template>
<script>
    // ...
    goDetail() {
        this.$router.push({
            name: 'course-detail',
            query: {
                id: this.course.id
            }
        });
    }
</script>
接收.vue
created() {
    let id = this.$route.query.id;
}
export default new Vuex.Store({
    state: {
        title: '默认值'
    },
    mutations: {
        // mutations 为 state 中的属性提供setter方法
        // setter方法名随意,可是参数列表固定两个:state, newValue
        setTitle(state, newValue) {
            state.title = newValue;
        }
    },
    actions: {}
})

赋值
this.$store.state.title = 'newTitle'
this.$store.commit('setTitle', 'newTitle')

取值
console.log(this.$store.state.title)

vue-cookie插件

安装css

>: cnpm install vue-cookies

main.js 配置vue

// 第一种
import cookies from 'vue-cookies'   // 导入插件
Vue.use(cookies);                   // 加载插件
new Vue({
    // ...
    cookies,                        // 配置使用插件原型 $cookies
}).$mount('#app');

// 第二种
import cookies from 'vue-cookies'   // 导入插件
Vue.prototype.$cookies = cookies;   // 直接配置插件原型 $cookies

使用node

// 增(改): key,value,exp(过时时间)
// 1 = '1s' | '1m' | '1h' | '1d'
this.$cookies.set('token', token, '1y');

// 查:key
this.token = this.$cookies.get('token');

// 删:key
this.$cookies.remove('token');

注:cookie通常都是用来存储token的python

// 1) 什么是token:安全认证的字符串
// 2) 谁产生的:后台产生
// 3) 谁来存储:后台存储(session表、文件、内存缓存),前台存储(cookie)
// 4) 如何使用:服务器先生成反馈给前台(登录认证过程),前台提交给后台完成认证(须要登陆后的请求)
// 5) 先后台分离项目:后台生成token,返回给前台 => 前台本身存储,发送携带token请求 => 后台完成token校验 => 后台获得登录用户

axios插件

安装
>: cnpm install axios
main.js配置
import axios from 'axios'   // 导入插件
Vue.prototype.$axios = axios;   // 直接配置插件原型 $axios
使用
this.axios({
    url: '请求接口',
    method: 'get|post请求',
    data: {post等提交的数据},
    params: {get提交的数据}
}).then(请求成功的回调函数).catch(请求失败的回调函数)

跨域问题(同源策略)

// 后台接收到前台的请求,能够接收前台数据与请求信息,发现请求的信息不是自身服务器发来的请求,拒绝响应数据,这种状况称之为 - 跨域问题(同源策略 CORS)

// 致使跨域状况有三种
// 1) 端口不一致
// 2) IP不一致
// 3) 协议不一致

// Django如何解决 - django-cors-headers模块
// 1) 安装:pip3 install django-cors-headers
// 2) 注册:
INSTALLED_APPS = [
    ...
    'corsheaders'
]
// 3) 设置中间件:
MIDDLEWARE = [
    ...
    'corsheaders.middleware.CorsMiddleware'
]
// 4) 设置跨域:
CORS_ORIGIN_ALLOW_ALL = True

element-ui插件

安装
>: cnpm i element-ui -S
main.js配置
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);
使用
依照官网 https://element.eleme.cn/#/zh-CN/component/installation api
相关文章
相关标签/搜索