NetlifyCMS 在回答此问题时(v2.1.3)不支持成为您的反应应用程序中的组件。只要您不尝试使用 Link 使用 react-router,就可以使用 react-router 将其添加到您的反应应用程序中。 NetlifyCMS 是它自己的 react 应用程序,并且有自己的路由和样式,如果您现在将其作为组件导入,则会干扰您的 react 应用程序。
创建create-react-app
npx create-react-app netlify-cms-cra-simple
cd netlify-cms-cra-simple
将 NetlifyCMS 应用程序的资产添加到 public/admin
public/admin/index.html
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Content Manager</title>
</head>
<body>
<!-- Include the script that builds the page and powers Netlify CMS -->
<script src="https://unpkg.com/netlify-cms@^2.0.0/dist/netlify-cms.js"></script>
</body>
</html>
public/admin/config.yml
backend:
name: github
repo: talves/netlify-cms-cra-simple
media_folder: "public/images"
public_folder: "images"
collections:
- name: settings
label: Settings
files:
- file: public/manifest.json
name: manifest
label: 'Manifest Settings'
fields:
- name: short_name
label: 'Short Name'
widget: string
required: true
- name: name
label: 'Name'
widget: string
required: true
- name: start_url
label: 'Start URL'
widget: string
default: '.'
required: true
- name: display
label: 'Display Type'
widget: string
default: 'standalone'
required: true
- name: theme_color
label: 'Theme Color'
widget: string
default: '#000000'
required: true
- name: background_color
label: 'Theme Color'
widget: string
default: '#FFFFFF'
required: true
- name: icons
widget: list
label: 'Icons'
allow_add: true
fields:
- widget: string
name: src
label: Source
- widget: string
name: sizes
label: 'Sizes'
- widget: string
name: type
label: 'Mime Type'
在public/images 为您的静态图像位置创建一个images 文件夹
更改config.yml 中repo 的名称并将更改提交到您的存储库。
使用 yarn start 启动您的 cra 应用,然后在浏览器中导航到 http://localhost:3000/admin/。
将 react-router-dom 依赖添加到您的项目 (yarn add react-router-dom)
将App 代码移动到名为Home 的新组件中。
新App.js
import React, { Component } from 'react';
import { BrowserRouter as Router, Switch, Route } from 'react-router-dom';
import Home from './Home';
class App extends Component {
render() {
return (
<Router>
<Switch>
<Route exact path="/" component={Home} />
</Switch>
</Router>
);
}
}
export default App;
注意: