目录标题
1 vue项目目录介绍
2 es6的导入导出语法
3 vue项目开发规范
4 vue项目集成axios,vue项目前后端打通
5 props配置项
6 混入
7 插件
8 scoped样式
9 localStorage和sessionStorage
10 集成elementui
1 vue项目目录介绍
myfirstvue
node_modules
public
- favicon. ico
- index. html
src
- assets
logo. png
- components
- HelloWorld. vue
- router
index. js
- store
index. js
- views
AboutView. vue
HomeView. vue
- App. vue
- main. js
. gitignore
babel. config. js
jsconfig. json
package. json
package- lock. json
README. md
vue. config. js
2 es6的导入导出语法
- 默认导出
- 命名导出
1 写一个js , 在js中定义变量,函数,最后使用export defalut 导出
export default {
name: '彭于晏' ,
printName ( ) {
console. log( this. name)
}
}
2 在想使用的js中,导入
import 随便起 from './lqz/utils'
1 写一个js , 在js中定义变量,函数,最后使用export 导出
export const name = '刘亦菲'
export const printName = ( ) = > {
console. log( '星象衔新宠' )
}
2 在想使用的js中,导入
import { printName} from './lqz/utils'
- 对比python中得__init__. py
3 vue项目开发规范
- < template> < / template>
- < script> < / script>
- < style> < / style>
1 把App. vue根组件导入了,
2 使用new Vue( { render: h = > h( App) } ) . $mount( '#app' ) 把App. vue组件中得数据和模板,插入到了index. html的id 为app div中了
3 以后,只要在每个组件的export default { } 写之前学过的所有js的东西
4 以后,只要在每个组件的template,写模板,插值语法,指令。。
5 以后,只要在每个组件的style,写样式
4 vue项目集成axios,vue项目前后端打通
- npm install axios - - S
import axios from 'axios'
axios. get( 'http://127.0.0.1:8000/books/' ) . then( res = > {
console. log( res. data)
this. bookList= res. data
} )
1 pip3 install django- cors- headers
2 app中注册:
INSTALLED_APPS = (
. . .
'corsheaders' ,
. . .
)
3 中间件注册
MIDDLEWARE = [
. . .
'corsheaders.middleware.CorsMiddleware' ,
. . .
]
4 把下面的复制到配置文件中
CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_METHODS = (
'DELETE' ,
'GET' ,
'OPTIONS' ,
'PATCH' ,
'POST' ,
'PUT' ,
'VIEW' ,
)
CORS_ALLOW_HEADERS = (
'XMLHttpRequest' ,
'X_FILENAME' ,
'accept-encoding' ,
'authorization' ,
'content-type' ,
'dnt' ,
'origin' ,
'user-agent' ,
'x-csrftoken' ,
'x-requested-with' ,
'Pragma' ,
)
4.1 前后端交互版登录功能
5 props配置项
- 1 数组写法
- 2 对象对象写法
- 3 对象套对象写法
// 普通使用
// props: [ 'msg' ] ,
// 属性验证
// props: { msg: String} ,
// 指定类型,必填和默认值
props: {
msg: {
type : String, // 类型
required: true, // 必要性
default: '老王' // 默认值
}
} ,
6 混入
把多个组件中公用的东西,抽取出来,以后可以全局使用和局部使用
- 1 写个mixin/ index. js
export const hunhe = {
data( ) {
return {
name: '彭于晏'
}
} ,
methods: {
handlePrintName( ) {
alert( this. name)
}
} ,
}
- 2 局部导入:在组件中
import { hunhe} from "@/mixin" ;
mixins: [ hunhe, ]
- 3 全局使用,在main. js 中 以后所有组件都可以用
import { hunhe} from "@/mixin" ;
Vue. mixin( hunhe)
7 插件
本质:包含install方法的一个对象,install的第一个参数是Vue,第二个以后的参数是插件使用者传递的数据
- 写一个 plugins/ index. js
import Vue from "vue" ;
import axios from "axios" ;
import { hunhe} from '@/mixin'
export default {
install( miVue) {
// console. log( miVue)
// 1 vue 实例上放个变量
// Vue. prototype. $name = 'lqz' // 类比python,在类上放了一个属性,所有对象都能取到
// Vue. prototype. $ajax = axios
// 2 使用插件,加入混入
// 全局使用混入
// Vue. mixin( hunhe)
// 3 定义全局组件
// 4 定义自定义指令 v- lqz
Vue. directive( "fbind" , {
// 指令与元素成功绑定时(一上来)
bind( element, binding) {
element. value = binding. value;
} ,
// 指令所在元素被插入页面时
inserted( element, binding) {
element. focus( ) ;
} ,
// 指令所在的模板被重新解析时
update( element, binding) {
element. value = binding. value;
} ,
} ) ;
}
}
2 在main. js 中使用插件
import plugins from '@/plugins'
Vue. use( plugins) // 本子,使用use,会自动触发插件对象中得install
3 以后再组件中可以直接用插件中写的东西
8 scoped样式
9 localStorage和sessionStorage
永久存储:localStorage 不登录加购物车,没登录 搜索过的商品
关闭页面数据就没了( 临时存储) :sessionStorage
设定一个时间,到时候就过期:cookie
// 对象转json字符串
// JSON. stringify( person)
// json字符串转对象
// JSON. parse( )
localStorage操作
点我向localStorage放数据
点我获取localStorage数据
点我删除localStorage放数据
sessionStorage操作
点我向localStorage放数据
点我获取localStorage数据
点我删除localStorage放数据
cookie操作
点我向localStorage放数据
点我获取localStorage数据
点我删除localStorage放数据
10 集成elementui
- 饿了么团队:elementui
- iview
- 移动端的ui:vant
- 安装 npm i element- ui - S
- 在main. js中引入