vue全家桶+Koa2开发笔记(5)--nuxt
1. nuxt项目初始化报错
- 下面是使用 koa 模板方法初始化一个项目,使用该方法需要将 nuxt 的版本降至1.4.2;
- 官方 https://zh.nuxtjs.org/guide/installation 还要提供了脚手架工具,可用使用最新的nuxt2.0版本初始化一个项目。
$ vue init nuxt-community/koa-template <project-name>
$ cd <project-name>
$ npm run dev
<!--
1. 如果有报错: Plugin/Preset files are not allowed to export objects, only functions
需要降低nuxt版本至1.4.2:
npm uninstall nuxt
npm install nuxt@1.4.2 2. 升级eslint-plugin-html
$ npm i eslint-plugin-html@^3
-->
在server文件夹中定义koa2的路由信息,并且在server/index中挂载注册路由信息;
在layouts文件夹中定义模板文件;在pages文件夹下编辑前端页面;
2、使用layout模板
例如在layouts文件夹中设置search模板
<template>
<div>
<nuxt/>
<my-footer/>
</div>
</template> <script>
import MyFooter from '../components/Footer.vue' export default {
components: {
MyFooter
}
}
</script>
这里注意 <nuxt>相当于 vue-router 中的
<!-- 对应的组件内容渲染到router-view中 -->
<router-view></router-view>
然后在 page 文件中 引入该模板:
<script>
export default {
layout:'search',
}
</script>
3、自己定义接口并获取接口
定义接口在koa-router,获取接口数据在page vue中:
在server/interface/city 文件下,设置接口信息
const router = require('koa-router')()
router.prefix('/city')
router.get('/list', (ctx) => {
ctx.body = {
list:['北京','天津']
}
})
module.exports = router
然后在server/index.js文件下挂载注册:
const city = require('./interface/city')
app.use(city.routes(), city.allowedMethods())
以上为服务器端接口设置,接下来是前端页面请求数据:
<template>
<div>
<ul>
<li v-for="(item,idx) in cities" :key="idx"> {{item}} </li>
</ul>
</div>
</template>
<script>
import axios from 'axios'
export default {
layout:'search',
data(){
return {
cities:[]
}
},
// async mounted() { //该方法是前端渲染页面
// let cities;
// let {status,data} = await axios.get('/city/list');
// console.log(data.list); //在浏览器端出现
// if(status == 200){
// this.cities = data.list
// }
// },
async asyncData() { //再render之前 获取异步数据 是服务器端渲染页面
let cities;
let {status,data} = await axios.get('http://localhost:3000/city/list');//由于是服务端渲染,需写全路径
console.log(data.list);//在服务器端出现
if(status == 200){
return {
cities:data.list //return返回data同名数据
}
}
}
}
</script> <style scoped> </style>
4. 引入vuex 框架
注意引入vuex 使用普通方式的状态树,需要添加 store/index.js 文件,并对外暴露一个 Vuex.Store 实例:
import Vue from 'vue';
import Vuex from 'vuex';
import citys from './modules/city'
import navbars from './modules/navbar' Vue.use(Vuex)//安装注册vuex const store = () => new Vuex.Store({//实例话vuex,并引入两个vuex定义的函数
modules:{
citys,
}
})
export default store
如上所示,需要实例化 vuex再抛出,和之前直接抛出 vuex的不一样。
5 见文章 Nuxt开发经验分享,让你踩少点坑
vue全家桶+Koa2开发笔记(5)--nuxt的更多相关文章
- vue全家桶+Koa2开发笔记(6)--app开发
1.环境配置 详见文章<Nuxt 开发 - 项目初始化> 1.1 使用nuxt脚手架 https://zh.nuxtjs.org/guide/installation 1.2 在nod ...
- vue全家桶+Koa2开发笔记(7)--登陆注册功能
1 文件结构:pages中放置页面代码:server 分为 dbs 和interface两个文件夹: dbs设置有关数据库的代码:interface设置接口信息: 2.2 先看dbs的,在dbs的配置 ...
- vue全家桶+Koa2开发笔记(2)--koa2
1. 安装koa脚手架的时候 执行命令 koa2 -e koa-learn 注意要使用-e的方式,才会生成ejs的模板 2. async await的使用方法:存在的意义:提高promise的可读性 ...
- vue全家桶+Koa2开发笔记(8)--开发网页
1.使用 mongoose 动态倒入数据 mongoimport -d student -c areas areas.dat -d 后面是数据库名称: -c后面是表名称 最后是数据源 2.使用vue的 ...
- vue全家桶+Koa2开发笔记(1)--vuex
1. 安装webpack的问题: webpack坑系列--安装webpack-cli 2. vue-cli(vue脚手架)超详细教程 3. 在命令行中使用 touch 执行新建文件: 4. 关 ...
- vue全家桶+Koa2开发笔记(4)--redis
redis用来在服务器端存放session 1 安装redis brew install redis 启动redis redis-server 2 安装两个中间件 npm i koa-ge ...
- vue全家桶+Koa2开发笔记(3)--mongodb
1. 安装 momgodb brew install mongodb安装成功后执行 which mongod启动:mongod 2. 下载可视化操作数据库的软件 https://robomongo.o ...
- Vue 全家桶 + Electron 开发的一个跨三端的应用
代码地址如下:http://www.demodashi.com/demo/11738.html GitHub Repo:vue-objccn Follow: halfrost · GitHub 利用 ...
- [在线+源码]vue全家桶+Typescript开发一款习惯养成APP
# vue-ts-daily 基于Vue.js的2.5.13版本和TypeScript编写的模仿原生应用的WebApp. [源码地址](https://github.com/xiaomuzhu/vue ...
随机推荐
- System.properties
win: len:54java.runtime.name=Java(TM) SE Runtime Environment sun.boot.library.path=D:\Java\jdk1.8. ...
- OPENWRT安装配置指南之 17.01.4 LEDE
简介 这个东西,需要看简介的就不要看下去了. 下面已刚刷进去,路由IP地址为192.168.1.1为例开始配置. 浏览器访问192.168.1.1,无密码. 一:配置上网 不管你是什么方式上网,请根据 ...
- 如何从零安装Mysql
1.yum/rpm安装 2.采用二进制方式免编译安装MySQL. 3.考虑到MySQL5.4.xx及以后系列产品的特殊性,其编译方式和早期的第一条产品线的有所不同,这里采用cmake或gmake方式的 ...
- const & define & inline
0x01 const & define区别 宏定义#define发生在预编译期,而const,enum定义的常量发生在编译期,两者的重要差别在于编译期里的变量是进符号表的,而预编译期的宏是简 ...
- windows文件映射
0x01 使用文件映射实现共享内存. 用内存映射文件实现进程间的通讯:Windows中的内存映射文件的机制为我们高效地操作文件提供了一种途径,它允许我们在进程中保留一段内存区域,把硬盘或页文件上的目标 ...
- Windows下安装Tensorflow报错 “DLL load failed:找不到指定的模块"
Windows下安装完tensorflow后,在cmd下运行python后import tensorflow出现如下错误: Traceback (most recent call last): Fi ...
- 预期结果 参数化parametrize
1.pytest.mark.parametrize装饰器可以实现测试用例参数化. 2.实例: import pytest @pytest.mark.parametrize("req,expe ...
- chromium ④
chromium的源码非常大,选择合适的点入手能省不少力气.在 win7下编译chromium中我曾提到学习chromium源码的一个小工程test_shell,代码目录在src/webkit/too ...
- TDate赋值给Variant时注意的问题
//Delphi 10.3.1 32bit, Win 7 32bit procedure TForm1.btnTimeToVariantClick(Sender: TObject); var d:TD ...
- SQL注入之Sqli-labs系列第七篇(基于root权限读写注入)
开始挑战第一关(Error Based- String) 开始之前,先介绍这关需要用到的几个函数,如下图 继续访问我们的地址,输入' “ 一些测试语句发现都没有反应 查看源码,是采用了单引号和双括号 ...