可参考该链接
vue脚手架开发篇(六)
vue ui
$ npm run serve
> [email protected] serve E:\learn\vue\test
> vue-cli-service serve
INFO Starting development server...
98% after emitting CopyPlugin
DONE Compiled successfully in 7381ms 1:55:49 ├F10: PM┤
App running at:
- Local: http://localhost:8081/
- Network: http://10.41.152.51:8081/
import Vue from 'vue'
import {
NavBar, Tabbar, TabbarItem } from 'vant'
Vue.use(NavBar)
.use(Tabbar)
.use(TabbarItem)
<template>
<div id="app">
<van-nav-bar
title="标题"
left-text="返回"
right-text="按钮"
left-arrow
@click-left="onClickLeft"
/>
<van-tabbar v-model="active">
<van-tabbar-item icon="home-o">标签van-tabbar-item>
<van-tabbar-item icon="search" dot>标签van-tabbar-item>
<van-tabbar-item icon="friends-o" badge="5">标签van-tabbar-item>
<van-tabbar-item icon="setting-o" badge="20">标签van-tabbar-item>
van-tabbar>
div>
template>
<script>
export default {
data: () => {
return {
active: 0
}
},
methods: {
onClickLeft () {
}
}
}
script>
<style>
style>
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const routes = [
{
path: '/',
redirect: '/index'
},
{
path: '/index',
component: () => import('../views/Index')
},
{
path: '/search',
component: () => import('../views/Search')
},
{
path: '/carts',
component: () => import('../views/Carts')
},
{
path: '/friends',
component: () => import('../views/Friends')
}
]
const router = new VueRouter({
routes
})
export default router
<van-tabbar v-model="active">
<van-tabbar-item icon="home-o" to="/index">首页van-tabbar-item>
<van-tabbar-item icon="search" dot to="/search">查找van-tabbar-item>
<van-tabbar-item icon="friends-o" badge="5" to="/friends">朋友van-tabbar-item>
<van-tabbar-item icon="setting-o" badge="20" to="/carts">购物车van-tabbar-item>
van-tabbar>
<router-view>router-view>
>cnpm i axios -S
√ Installed 1 packages
√ Linked 1 latest versions
√ Run 0 scripts
√ All packages installed (2 packages installed from npm registry, used 459ms(network 453ms), speed 20.09kB/s, json 2(9.1kB), tarball 0B)
import Vue from 'vue'
import axios from 'axios'
axios.defaults.baseURL = 'http://127.0.0.1:8888'
// 获取轮播数据
const getLunbo = params => {
return axios.get('/api/getlunbo')
}
// 获取九宫格
const getGrids = params => {
return axios.get('/api/girds')
}
// 暴露接口
Vue.prototype.$http = {
getLunbo,
getGrids
}
import './api'
import Vue from 'vue'
import {
NavBar, Tabbar, TabbarItem, Swipe, SwipeItem } from 'vant'
Vue.use(NavBar)
.use(Tabbar)
.use(TabbarItem)
.use(Swipe)
.use(SwipeItem)
<van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
<van-swipe-item>1van-swipe-item>
<van-swipe-item>2van-swipe-item>
<van-swipe-item>3van-swipe-item>
<van-swipe-item>4van-swipe-item>
van-swipe>
>cnpm i less less-loader -S
√ Installed 2 packages
√ Linked 26 latest versions
√ Run 0 scripts
Recently updated (since 2020-10-06): 3 packages (detail see file E:\learn\vue\test\node_modules\.recently_updates.txt)
√ All packages installed (28 packages installed from npm registry, used 5s(network 4s), speed 227.92kB/s, json 28(91.9kB), tarball 932.59kB)
export default {
data: () => {
return {
lunboList: []
}
},
created () {
this.getlunbo()
},
methods: {
async getlunbo () {
const {
data: {
message } } = await this.$http.getLunbo()
this.lunboList = message
}
}
}
<van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
<van-swipe-item v-for="el in lunboList" :key="el.id"><img :src="el.img" alt="">van-swipe-item>
van-swipe>
<template>
<div>
<van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
<van-swipe-item v-for="el in lunboList" :key="el.id"><img :src="el.img" alt=""></van-swipe-item>
</van-swipe>
<van-grid :column-num="3">
<van-grid-item v-for="value in grids" :key="value.id" :icon="value.src" :text="value.title" />
</van-grid>
</div>
</template>
<script>
export default {
data: () => {
return {
lunboList: [],
grids: []
}
},
created () {
this.getlunbo()
this.getGrids()
},
methods: {
async getlunbo () {
const {
data: {
message } } = await this.$http.getLunbo()
this.lunboList = message
},
async getGrids () {
const {
data: {
message } } = await this.$http.getGrids()
this.grids = message
}
}
}
</script>
<style lang="less" scoped>
.my-swipe .van-swipe-item {
color: #fff;
font-size: 20px;
height: 150px;
text-align: center;
background-color: #39a9ed;
}
img{
height: 100%;
width: 100%;
}
</style>
{
path: '/home/newslist/:id',
component: () => import('../views/Index/News/NewsInfo')
}
// 通过id获取信息的详细
const getNewsInfoById = params => {
return axios.get('/api/getnew/' + params)
}
async getDetails (id) {
this.$router.push('/home/newslist/' + id)
}
this.newsId = this.$route.params.id
async getNewsByIds () {
const {
data: {
message } } = await this.$http.getNewsInfoById(this.newsId)
this.data = message
}
<div class="newsInfo">
<header class="header">
<h1 class="title">{
{data.title}}h1>
<div class="btn-group">
<span>{
{data.add_time}}span>
<span>点击{
{data.click}}次span>
div>
<hr>
header>
<div class="content" v-text="data.content">div>
<Comment :id="newsId">Comment>
div>
// 获取评论的接口
const getComments = ({
artid, page, pageSize = 3 }) => {
return axios.get(`/api/getcomments/${
artid}?pageindex=${
page}&limit=${
pageSize}`)
}
// 添加评论的接口
const postComments = ({
id, content }) => {
return axios.post('/api/postcomment/' + id, {
content })
}
async getComment () {
if (this.flag === true) return
this.page++
try {
const {
data: {
message, count } } = await this.$http.getComments({
artid: this.id, page: this.page, pageSize: this.pageSize })
this.comments = this.comments.concat(message)
this.flag = this.page * this.pageSize > count
} catch (error) {
this.$Toast(error.message)
}
},
import Comment from '@/components/Comment'
async postComment () {
try {
const {
data: message } = await this.$http.postComments({
id: this.id, content: this.content })
this.comments.unshift(
{
user_name: '匿名用户',
add_time: new Date().getTime(),
content: this.content
})
console.log(message)
} catch (error) {
console.log(error.message)
}
}