云起工作室 15711107967

02生命周期

1、生命周期 <!-- 服务端钩子 --> Incoming Request nuxtServerInit 在 store/index.js 的export const actions={}里 验证是否有登录状态,如cookie,并奖cookie 放到store的state里,也就是拿到登录信息 middleware 在根目录下创建middleware文件夹,创建任意名称js文件,如auth.js ,可在nuxt.config.js 的router 里引用,这是全局路由,也可在页面里单独使用,验证登录信息是否存在,并作跳转处理 walidate 在页面里可已验证 是否有必要的参数 asyncData() & fetch() asyncData只可在页面中使用,不可在组件中使用,fetch 即可在页面中使用,也可在组件中使用,填充store数据多用于fetch Render Vue 的生命周期 beforeCreated && created  同时运行在客户端和服务端 <!-- 客服端钩子 --> beforeMount(){} mounted(){} beforeUpdate(){} updated(){} beforeDestory(){} destoryed(){} 2、nuxtServerInit 生命周期 在 store下的index.js 里 actions 里 export const actions = {     nuxtServerInit(store,context){         <!-- 这里可以做一些数据初始化 -->         console.log("nuxtServerInit",store,context)     } } 3、middleware 在nuxt.config.js 里配置 router:{     middleware:"auth"  //指定授权的中间件名 } 在middleware 文件夹下创建 auth.js export default (store,route,redirect,params,query,req,res)=>{     <!-- 全局守卫业务 --> //store 状态树 //route //redirect("/login") //强制跳转 //params,query 校验等 } 页面级别的middleware export default{     <!-- middleware:"auth"// 指向 middleware/auth.js  -->     <!-- 或者 -->     middleware(){         <!-- 页面守卫 -->     } } 4、validate     export default{         <!-- 主要用于对参数的校验 -->         validate({params,query}){             return true  //return false 会不显示该页面         }     } 5、asyncData       返回数据 会与data合并     exprot default{         asyncData(context){             <!-- 读取接口 -->             return {}         },         <!-- fetch 也是读取接口 ,但会将数据提交给 vuex -->         fetch({store}){             <!-- 通过store api 提交给vuex -->         }     }

10七牛云上传文件

token 通过后端获取 安装skd npm install qiniu-js 报错 core-js 是版本不兼容问题 npm install core-js@2.x 解决 import * as qiniu from 'qiniu-js' 上传 var token="";             var file = e.target.files[0]             var name=Date.now();             var key="aisty/"+name+".jpg";             var observable = qiniu.upload(file, key, token)             var observer = {                 next(res){                     // ...                 },                 error(err){                 },                 complete(res){                     //上传完成后的逻辑 res.key 是上传的文件路径及名称 也就是key                 }             }             var subscription = observable.subscribe(observer) // 上传开始 上传base64 数据 upCover(file){             var _this = this             var pic = file.split(",")[1]; //只需要 逗号后边的部分             var name=Date.now();             var key="aisty/"+name+".png";                   //非华东空间需要根据注意事项 1 修改上传域名 ,key 是自定义的名称,需要用七牛云的 urlSafeBase64Encode 一下             var url = "http://upload-z2.qiniup.com/putb64/-1/key/"+qiniu.urlSafeBase64Encode(key);             var xhr = new XMLHttpRequest();             xhr.onreadystatechange=function(){                 if (xhr.readyState==4){                     var res = JSON.parse(xhr.responseText)                     console.log(res)                 }             }             xhr.open("POST", url, true);             xhr.setRequestHeader("Content-Type", "application/octet-stream");             xhr.setRequestHeader("Authorization", "UpToken "+this.qiniuToken); // UpToken 和 七牛的token之间要有空格             xhr.send(pic); }


01安装部署

1、安装 npx create-nuxt-app xxx 或npx nuxi init xxx 一定要选择 Choose rendering mode Universal (SSR) 开发: npm run dev yarn dev 打包: npm run build npm run start yarn build yarn start 2、部署: 将 .nuxt文件夹、server文件夹、nuxt.config.js、package-lock.json、package.json 放到云服务器 nuxt.config.js module.exports={ server:{ prot:3000,//端口 host:'0.0.0.0' } } 执行 npm install 安装依赖包 或直接将 node_modules 一并放到云服务器 用pm2 管理 安装 pm2 npm install -g pm2 (node做后端的接口服务)开启后端:pm2 start ./bin/www --name=nuxt9000 开启前端:pm2 --name=nuxt3000 start npm -- run start 查看是否监听端口 netstat -nltp pm2 npm install pm2 -g 安装 pm2 update 更新版本 pm2 list 显示所有进程状态 pm2 monit 监视所有进程 pm2 log 显示所有进程日志 pm2 stop all 停止所有进程 pm2 restart all 重启所有进程 pm2 reload all 0 秒停机重载进程 (用于 NETWORKED 进程) pm2 stop name 停止指定的进程,name为标识 pm2 restart name 重启指定的进程,name为标识 pm2 startup 产生 init 脚本 保持进程活着 pm2 web 运行健壮的 computer API endpoint (http://localhost:9615) pm2 delete name 杀死指定的进程,name为标识 pm2 delete all 杀死全部进程 3、安装axios 和 proxy npm i @nuxtjs/axios @nuxtjs/proxy 配置nuxt.config.js modules:[ '@nuxtjs/axios' ], axios:{ proxy:true, //开启代理 prefix:"/api" }, //代理配置 proxy:{ "/api/":{ target:"http://localhost:8080", changeOrigin:true, pathRewrite:{ //'^/api':'' } } } 配置端口 server:{     port:3001,     host:'0.0.0.0'   }, linux下安装nvm 下载压缩包 https://github.com/nvm-sh/nvm/archive/refs/tags/v0.38.0.tar.gz wget https://github.com/nvm-sh/nvm/archive/refs/tags/v0.38.0.tar.gz 解压 mkdir -p /root/.nvm tar -zxvf nvm-0.38.0.tar.gz -C /root/.nvm 修改 ~/.bashrc 添加以下内容 export NVM_DIR="$HOME/.nvm/nvm-0.38.0" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion # nodejs下载更换淘宝镜像 export NVM_NODEJS_ORG_MIRROR=https://npm.taobao.org/mirrors/node



vue -quill-editor

npm install vue-quill-editor --save2、新建plugins/vue-quill-editor.js文件import Vue from 'vue' import VueQuillEditor from 'vue-quill-editor/dist/ssr' Vue.use(VueQuillEditor); 3、配置nuxt.config.jsmodule.exports = { // other config // ... // import css files as global style css: [ 'quill/dist/quill.snow.css', 'quill/dist/quill.bubble.css', 'quill/dist/quill.core.css' ], plugins: [ // other plugins { src: '~/plugins/vue-quill-editor', ssr: false } ] } 4、组件中使用<template> <div> <section class="container"> <div class="quill-editor" :content="content" @change="onEditorChange($event)" @blur="onEditorBlur($event)" @focus="onEditorFocus($event)" @ready="onEditorReady($event)" v-quill:myQuillEditor="editorOption"> </div> </section> <div class="editor-uploader">     <input type="file" accept="image/*" @change="onUploadHandler"/>  </div> </div> </template> <script> export default { data () { return { content: '<p>I am Example</p>', editorOption: { // some quill options modules: { toolbar: [ ['bold', 'italic', 'underline', 'strike'], // 加粗 斜体 下划线 删除线           ['blockquote', 'code-block'], // 引用  代码块           [{ header: 1 }, { header: 2 }], // 1、2 级标题           [{ list: 'ordered' }, { list: 'bullet' }], // 有序、无序列表           [{ script: 'sub' }, { script: 'super' }], // 上标/下标           [{ indent: '-1' }, { indent: '+1' }], // 缩进           [{ direction: 'rtl' }], // 文本方向           [{ size: ['12', '14', '16', '18', '20', '22', '24', '28', '32', '36'] }], // 字体大小           [{ header: [1, 2, 3, 4, 5, 6] }], // 标题           [{ color: [] }, { background: [] }], // 字体颜色、字体背景颜色           // [{ font: ['songti'] }], // 字体种类           [{ align: [] }], // 对齐方式           ['clean'], // 清除文本格式           ['image', 'video'] // 链接、图片、视频 ], handlers: {           'image': function (value) {             if (value) { // value === true               document.querySelector('.editor-uploader input').click()                         }           }         } } } } }, methods: { onEditorBlur(editor) { console.log('editor blur!', editor) }, onEditorFocus(editor) { console.log('editor focus!', editor) }, onEditorReady(editor) { console.log('editor ready!', editor) }, onEditorChange({ editor, html, text }) { console.log('editor change!', editor, html, text) this.content = html }, // 编辑器上传图片     async onUploadHandler(e) {             var file = e.target.files[0]             var _this = this             var param = new Object()             param.type=conf.fileType.editImage             param.obj_id=this.id             conf.upfileCallback(this.$axios,file,qiniu,param,(res)=>{                                 const imageUrl = conf.qiniuHost+res.key                           // 获取光标所在位置                 let quill = _this.myQuillEditor                                 let length = quill.getSelection().index                                 // 插入图片                   quill.insertEmbed(length, 'image', imageUrl)                 // // 调整光标到最后                 quill.setSelection(length + 1)             })                 }, } } </script> <style lang="scss" scoped> .container { width: 60%; margin: 0 auto; padding: 50px 0; .quill-editor { min-height: 200px; max-height: 400px; overflow-y: auto; } } </style>