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的更多相关文章

  1. vue全家桶+Koa2开发笔记(6)--app开发

    1.环境配置 详见文章<Nuxt 开发 - 项目初始化> 1.1  使用nuxt脚手架  https://zh.nuxtjs.org/guide/installation 1.2 在nod ...

  2. vue全家桶+Koa2开发笔记(7)--登陆注册功能

    1 文件结构:pages中放置页面代码:server 分为 dbs 和interface两个文件夹: dbs设置有关数据库的代码:interface设置接口信息: 2.2 先看dbs的,在dbs的配置 ...

  3. vue全家桶+Koa2开发笔记(2)--koa2

    1. 安装koa脚手架的时候 执行命令 koa2 -e koa-learn 注意要使用-e的方式,才会生成ejs的模板 2. async await的使用方法:存在的意义:提高promise的可读性 ...

  4. vue全家桶+Koa2开发笔记(8)--开发网页

    1.使用 mongoose 动态倒入数据 mongoimport -d student -c areas areas.dat -d 后面是数据库名称: -c后面是表名称 最后是数据源 2.使用vue的 ...

  5. vue全家桶+Koa2开发笔记(1)--vuex

    1.  安装webpack的问题: webpack坑系列--安装webpack-cli 2.  vue-cli(vue脚手架)超详细教程 3.  在命令行中使用 touch 执行新建文件: 4.  关 ...

  6. vue全家桶+Koa2开发笔记(4)--redis

    redis用来在服务器端存放session 1 安装redis    brew install redis 启动redis   redis-server 2 安装两个中间件  npm i koa-ge ...

  7. vue全家桶+Koa2开发笔记(3)--mongodb

    1. 安装 momgodb brew install mongodb安装成功后执行 which mongod启动:mongod 2. 下载可视化操作数据库的软件 https://robomongo.o ...

  8. Vue 全家桶 + Electron 开发的一个跨三端的应用

    代码地址如下:http://www.demodashi.com/demo/11738.html GitHub Repo:vue-objccn Follow: halfrost · GitHub 利用 ...

  9. [在线+源码]vue全家桶+Typescript开发一款习惯养成APP

    # vue-ts-daily 基于Vue.js的2.5.13版本和TypeScript编写的模仿原生应用的WebApp. [源码地址](https://github.com/xiaomuzhu/vue ...

随机推荐

  1. 【阅读笔记】《C程序员 从校园到职场》第二章 学校到职场

    一.代码规范: 1.变量命名(让人一眼看它是什么意思,要做什么操作),定义并初始化 2.函数命名规范(函数的功能)在主函数之前进行声明. 在实际项目中,一般不在函数调用者的内部来对被调函数进行声明,而 ...

  2. idea查看jar包是否存在

    idea在project目录下如下图(1),是总的pom文件,定义了四个子模块共用的依赖,并且其中定义了四个子模块,,每个模块都有各自的pom.xml文件.结构目录只有一个总的lib库. 但是可能在s ...

  3. rancher中使用ingress-lbs做负载均衡

    rancher 相关资料 http://rancher.com/docs/rancher/v1.6/zh/kubernetes/ingress/ lvs, haproxy, nginx负载均衡器比较 ...

  4. Spring学习五(JDBC支持)

    Spring的jdbc支持 1配置db.properties,将有关JDBC的信息载入 2bean文件配置数据源,这里用e3p0作为数据源载入db.properties 3配置template的bea ...

  5. leetcode31题:下一个排列

    实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须原地修改,只允许使用额外常数空间. ...

  6. windwos7 vnc连接centos6.6

    一.先配置centos6.6的vnc(已经安装过桌面) #yum  install  fontforge -y   防止字体乱码 #yum tigervnc tigervnc-server -y   ...

  7. mod_fcgid: HTTP request length 136136 (so far) exceeds MaxRequestLen (131072)

    原来是fastcgi模式下的设置问题,需要在配置文件.htaccess或者直接在apache的配置文件http.conf 中指明,如下: 查看官方说明有这么一句:Default: FcgidMaxRe ...

  8. c++ 编译报错汇总(随时更新)

    1.invalid new-expression of abstract class type ‘×××ב 这个报错代表一个尝试在实例化一个抽象类,也就是说父类的接口中有纯虚函数在子类中没有实现: ...

  9. Spring Boot 揭秘与实战(二) 数据缓存篇 - Redis Cache

    文章目录 1. Redis Cache 集成 2. 源代码 本文,讲解 Spring Boot 如何集成 Redis Cache,实现缓存. 在阅读「Spring Boot 揭秘与实战(二) 数据缓存 ...

  10. Unity 3D UGUI Toggle用法教程

    UGUI Toggle用法教程 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分 ...