本文为拉勾网大前端高薪训练营第一期笔记
需要有一定的Vue基础
gridsome.config.js
https://gridsome.org/docs/config/
module.exports = {
siteName: '拉勾教育',
siteDescription: '大前端',
plugins: []
}
siteName是网页title "a - b"这里的b,同时也是header左上角的名称
siteDescription是网页meta description
https://gridsome.org/docs/pages/
两种选项创建页面,一种是文件系统
src/pages/Index.vue
becomes /
(The frontpage)src/pages/AboutUs.vue
becomes /about-us/
src/pages/about/Vision.vue
becomes /about/vision/
src/pages/blog/Index.vue
becomes /blog/
动态路由式
src/pages/user/[id].vue
becomes /user/:id
.src/pages/user/[id]/settings.vue
becomes /user/:id/settings
.一种是编程式
gridsome.server.js
module.exports = function (api) {
api.createPages(({ createPage }) => {
createPage({
path: '/my-page',
component: './src/templates/MyPage.vue'
})
})
}
动态路由例子
module.exports = function (api) {
api.createPages(({ createPage }) => {
createPage({
path: '/user/:id(\\d+)',
component: './src/templates/User.vue'
})
})
}
另外可以配置动态路由
https://gridsome.org/docs/dynamic-routing/
每页的meta info,gridsome通过vue-meta配置的
Hello, world!
自己定义的404页面放这个路径
src/pages/404.vue
模拟数据接口 jsonplaceholder.typicode.com
Data Store API添加集合
// gridsome.server.js
const axios = require('axios')
module.exports = function (api) {
api.loadSource(async actions => {
const collection = actions.addCollection('Post')
const { data } = await axios.get('')
for (const item of data) {
collection.addNode({
id: item.id,
title: item.title,
content: item.content
})
}
})
}
然后检查的方式是打开
//localhost:8080/___explore
//左边输入
query {
post (id: 1) {
id
title
content
}
}
//或者查看所有文字
query {
allPost {
edges {
node {
id
title
}
}
}
}
//具体结构可以看右侧的docs
https://gridsome.org/docs/querying-data/
举例
{
{ edge.node.title }}
query {
posts: allWordPressPost {
edges {
node {
id
title
path
}
}
}
}
想看静态外部数据的话,得npm run build看index.html才能看出效果
//gridsome.config.js
module.exports = {
templates: {
Post: {
path: '/posts/:id',
components: './src/templates/Post.vue'
}
}
}
//templates/Post.vue
// ID!表示不能为空
{
{ $page.post.title}}
{
{$page.post.content}}
query ($id: ID!) {
post (id: $id) {
id
title
content
}
}
创建项目
gridsome create blog-with-gridsome
npm run develop
fork https://github.com/StartBootstrap/startbootstrap-clean-blog
git clone https://github.com//startbootstrap-clean-blog --depth=1
npm i bootstrap @fortawesome/fontawesome-free
/src/main.js
import 'bootstrap/dist/css/bootstrap.css'
import '@fortawesome/fontawesome-free/css/all.min.css'
import './assets/css/index.css'
/src/assets/css/index.css
@import url("https://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic");
@import url("https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800");
copy startbootstrap-clean-blog/css/clean-blog.css content to /src/assets/css/index.css
copy startbootstrap-clean-blog/index.html page-header/main-content
to src/pages/index.vue template
copy startbootstrap-clean-blog/img to static
add leading / to "/img/home-bg.jpg" in index.vue
copy src/pages/index.vue navigation/footer to src/layouts/Default.vue template
navigation 和 footer 中继加
给pages/index.vue 加 Layout
删除page-query和styles
add pages/Post.vue
copy startbootstrap-clean-blog/post.html page-header/post-content to Post.vue
copy startbootstrap-clean-blog/about.html page-header/about-content to About.vue
add pages/Contact.vue
copy startbootstrap-clean-blog/contact.html page-header/main-content to About.vue
使用本地md文件当做数据来源
https://gridsome.org/docs/fetching-data/#markdown
//gridsome.config.js
module.exports = {
plugins: [
{
use: '@gridsome/source-filesystem',
options: {
path: 'blog/**/*.md',
typeName: 'Post',
},
},
],
};
strapi介绍
strapi CMS系统
https://strapi.io
跑起来可以通过页面上的内容类型生成器来添加类型
可以创建用户,设置权限,并且登陆后获取jwt token,header里放Authorization: Bearer
可以设置role,然后给用户关联role,这样做权限控制可以方便控制哪种role可以创建更新,权限控制很自由。
通过graphQL访问Strapi
https://strapi.io/documentation/3.0.0-beta.x/plugins/graphql.html
yarn strapi install graphql
可以创建tag这种数据类型,然后和post多对多关联
Gridsome用strapi的数据
安装@gridsome/source-strapi插件
分页
https://gridsome.org/docs/pagination/
//gridsome built-in pager component example usage
-
{
{ edge.node.title }}
query ($page: Int) {
allBlogPost(perPage: 10, page: $page) @paginate {
pageInfo {
totalPages
currentPage
}
edges {
node {
id
title
}
}
}
}
展示文章详情
//gridsome.config.js
module.exports = {
siteName: '拉勾教育',
siteDescription: '大前端',
plugins: [
{
use: 'gridsome-source-strapi',
options: {
apiURL: `http://localhost:1337`,
queryLimit: 1000, // Defaults to 100
contentTypes: [`post`],
// typeName: 'Strapi',
// plural: true, // pluralizes names of Content Types in API
// Possibility to login with a Strapi user, when content types are not publicly available (optional).
// loginData: {
// identifier: '',
// password: '',
// },
}
}
],
templates: {
StrapiPost: [ //因为typeName+contentTypes
{
path: '/posts/:id',
component: './src/templates/Post.vue'
}
]
}
}
处理markdown
读来数据以后用markdown-it处理一下,methods里增加一个mdToHtml的方法,最后v-html的时候调用一下
文章标签
//gridsome.config.js
templates: {
StrapiTag: [
{
path: '/tags/:id',
component: './src/templates/Tag.vue'
}
]
}
//Tag.vue
//取数据部分
query ($id: ID!){
strapiTag (id: $id){
id
title
posts {
id
title
}
}
}
strapi里使用single types
唯一的数据类型,比如网站的主标题、副标题、封面
内容类型生成器里single type → create new single type
gridsome.config.js里plugins source-strapi singleTypes里设置你创建的类型
query {
allStrapiGeneral {
edges {
node {
id
title
subtitle
cover {
url
}
}
}
}
}
computed : {
general () {
return this.$page.general.edges[0].node
}
}
Gridsome环境设置
https://gridsome.org/docs/environment-variables/
创建两个文件
.env.development
.env.production
GRIDSOME_API_URL=https://api.example.com
DB_USER=root
DB_PASS=s1mpl3
使用的时候
process.env.DB_USER
另外css里的url可以通过以下方式拿到
//src/main.js
export default function (Vue, {router, head, isClient}) {
Vue.mixin({
data() {
return {
GRIDSOME_API_URL: ProcessingInstruction.env.GRIDSOME_API_URL
}
}
})
}
Vercel自动部署
https://vercel.com/
hook设置
在settings/git integration里
deploy hooks/create hook
hook name: deploy
git branch name: master
复制生成的钩子链接
然后去strapi设置里webhooks里添加
名称:deploy
请求地址:刚才复制的链接
事件挑需要的选