var app = new Vue({
//el 就是 element 不能使用html body 只能使用普通元素
el: '#app',
data: {
message: 'Hello Vue',
url: 'http://www.baidu.com/',
isLogin: true,
seen: true,
fruits: ['苹果','香蕉','菠萝'],
htmlStr: '你好
'
},
//事件名不得和data属性重名
methods:{
//dosomething:function(){}
dosomething()=>{
}
},
//计算属性
computed:{
dosome:() => 112
}
})
{{ messsage }}
{{ message }}
v-
前缀的特殊特性) 百度
//简写 可以省略 v-bind
百度
//文本域
//复选框 选中是否会改变isLogin
//简写 省略 v-on
seen 为 true 或者 false
现在你看到我了
与 v-model 结合使用
//点击复选框随即更改 seen
//根据 seen 判断是否渲染
现在你看到我了
-
{{item}}
//等同
{{ message }}
//css
[v-cloak] {
display: none;
}
//html
{{ message }}
hahah
{{ message }}
{{ this will not be compiled }}
Vue.component('my-component-name', {
template=``,
//功能
})
//引入 每个.vue都是一个组件
import App from './App.vue'
//注册
new Vue({
components: { App },
router,
template: ' '
}).$mount('#app')
<template>
<div>
//通过v-bind 动态赋值
<Header :user="user"></Header>
<router-view></router-view>
<Footer></Footer>
</div>
</template>
<script>
import Footer from './common/Footer.vue'
import Header from './common/Header.vue'
export default {
//导航守卫
async beforeRouteEnter(to,from,next){
try {
const {data:user} = await axios.get('/api/session')
next(vm => {
vm.user = user
})
} catch(e) {
// statements
console.log(e);
}
},
data() {
return {
user:null
}
},
components: {
Footer,
Header
}
}
</script>
<style>
/*css*/
</style>
子组件
<script>
export default {
//可以通过this访问user
//可以当作局部
props:['user'],
data() {
return {
//也可以这样来做 counter独立 也可以修改
counter:this.user
}
}
}
</script>
props验证 详细看官方文档 传送门
举例
//
props:{
user:{
type:Object,
default:null
}
}
//
props:{
user:[String,Number]
}
npm install vue-router
//组件引入
import VueRouter from 'vue-router'
import Register from './components/Register.vue'
import Home from './components/Home.vue'
import Login from './components/Login.vue'
import List from './components/common/List.vue'
import Create from './components/common/Create.vue'
import Topic from './components/common/Topic.vue'
import Edit from './components/common/Edit.vue'
import Aside from './components/common/profile/Aside.vue'
import Info from './components/common/profile/Info.vue'
import Admin from './components/common/profile/Admin.vue'
export default new VueRouter({
routes: [{
path: '/',
component: Home,
//子路由 如果你做sap页面会用到
children:[
{
path:'/',
component:List
},
{
path:'/Create',
component:Create
},
{
name:"topic",
path:'/topic/:id',
component:Topic
},
{
name:"edit",
path:'/edit/:id',
component:Edit
},
{
path:'/setting/info',
component:Aside,
children:[
{
path:'/setting/info',
component:Info
},
{
path:'/setting/admin',
component:Admin
}
]
}
]
},
{
path: '/register',
component: Register
},
{
path: '/login',
component: Login
}
]
})
//必须有to,from,next
async beforeRouteEnter(to,from,next){
try {
const {data:user} = await axios.get('/api/session')
//beforeRouterEnter 是在导航确认前被调用 组件还未创建 无法访问this 可用vm访问
//确认 next()才能进入你导航进入的组件 类似于设置一道关卡,next 放行
next(vm => {
vm.user = user
})
} catch(e) {
// statements
console.log(e);
}
}
json-server
来模拟数据接口npn i -g json-server
// 最后一个是 模拟数据的文件名 举例list.json
json-server --watch list.json
postman