時(shí)間:2023-09-17 01:06:02 | 來源:網(wǎng)站運(yùn)營(yíng)
時(shí)間:2023-09-17 01:06:02 來源:網(wǎng)站運(yùn)營(yíng)
二、前端開發(fā)—搭建靜態(tài)網(wǎng)站(:# 1.首先使用 npm 在全局安裝 create-react-appnpm install -g create-react-app# 2.在指定目錄中創(chuàng)建一個(gè) react 項(xiàng)目工程create-react-app my-blog# my-blog 是自己設(shè)置的項(xiàng)目名稱# 3.進(jìn)入到指定的項(xiàng)目文件,啟動(dòng)項(xiàng)目cd my-blognpm run start # 啟動(dòng)項(xiàng)目
my-blog├── node_modules : 存放 npm 安裝的工具包 或 模塊├── public : 一些靜態(tài)文件,一般不需要修改,除了網(wǎng)站標(biāo)題和icon,或者要添加一些特定的東西|── src : 項(xiàng)目的源代碼及資源| ├── api : 連接后端接口的 js 代碼| ├── assets : 存放公共資源| ├── component : 存放項(xiàng)目共用的組件(不會(huì)修改其他組件狀態(tài))| ├── containers : 存放項(xiàng)目共用的組件(將會(huì)修改其他組件狀態(tài)的組件)| ├── redux : 存放狀態(tài)管理組件的 js 代碼| └── index.js : js 的入口文件└── package.json : 項(xiàng)目的配置文件
yarn add antd
craco
bash yarn add @craco/yarn add craco-less
my-blog/package.json
中的 scripts
屬性 /* package.json */ "scripts": {- "start": "react-scripts start",- "build": "react-scripts build",- "test": "react-scripts test",+ "start": "craco start",+ "build": "craco build",+ "test": "craco test", }
my-blog/craco.config.js/
用于修改默認(rèn)配置。const CracoLessPlugin = require('craco-less');module.exports = { plugins: [ { plugin: CracoLessPlugin, options: { lessLoaderOptions: { lessOptions: { modifyVars: { '@primary-color': '#1DA57A' }, javascriptEnabled: true, }, }, }, }, ],};
bash yarn add @ant-design/icons
my-blog/src/index.js
入口文件:import React from 'react'import ReactDom from 'react-dom'import Main from "./components/main/main";import 'antd/dist/antd.less'; // 導(dǎo)入 antd 組件樣式ReactDom.render(<Main />, document.getElementById('root'))
在 my-blog/src/components/main/
中新建 main.jsx
文件:import React, {Component} from "react";import {Layout} from 'antd' const { Header, Footer, Content } = Layout;export default class Main extends Component{ render() { return ( <Layout> <Header> 導(dǎo)航欄 </Header> <Content> 內(nèi)容體 </Content> <Footer> 頁腳 </Footer> </Layout> ) }}
Layout、Header、Content、Footer 都是 antd 中做好的組件,我們只需要拿來用即可。my-blog/src/components/nav-top/
文件夾中新建 nav-top.jsx
寫入:import React, {Component} from "react";import {MailOutlined,EditFilled,TabletFilled} from '@ant-design/icons'import { Menu } from 'antd';export default class NavTop extends Component{ state = { navList : [ { path: '/index', text: '首頁', icon: <MailOutlined/> }, { path: '/project', text: '項(xiàng)目', icon: <EditFilled /> }, { path: '/diary', text: '日記', icon: <TabletFilled /> }, ] } render() { const {navList} = this.state return ( <div> <Menu theme={"dark"} mode="horizontal"> { navList.map((nav, index) => ( <Menu.Item icon={nav.icon} key={nav.path} selected={true}> {nav.text} </Menu.Item> )) } </Menu> </div> ) }}// Meau 是 antd 的導(dǎo)航菜單組件庫,它提供了許多的 api,theme 可以設(shè)置主題樣式、mode 設(shè)置菜單是橫向排列還是豎向排列。// 有多少個(gè) Menu.Item ,就有多少個(gè)菜單
然后在靜態(tài)博客框架 main.js 文件中導(dǎo)入 NavTop :import React, {Component} from "react";import {Layout} from 'antd'import NavTop from "../nav-top/nav-top";const { Header, Footer, Content } = Layout;export default class Main extends Component{ render() { return ( <Layout> <Header> <NavTop /> </Header> <Content> 內(nèi)容體 </Content> <Footer> 頁腳 </Footer> </Layout> ) }}
導(dǎo)航欄的效果就顯示出來了。my-blog/src/assets/css/
中新建 index.less
文件:// 修改導(dǎo)航欄背景樣式.ant-layout-header { background: #f0f2f5;}.ant-menu{ background: #f0f2f5;}// 修改導(dǎo)航欄字體樣式.ant-menu .anticon + span { color: #738192;}.ant-menu .anticon { color: #738192;}.ant-menu .ant-menu-item-selected .anticon + span { color: #20a0ff;}.ant-menu .ant-menu-item-selected .anticon { color: #20a0ff;}
在博客項(xiàng)目中導(dǎo)入 index.less:// my-blog/src/index.js 中導(dǎo)入 index.less...import 'antd/dist/antd.less'; // 導(dǎo)入 antd 組件樣式import './assets/css/index.less'; // 用于覆蓋上面 antd 定義的變量,必須放在 antd.less 下方...
在 my-blog/src/components/nav-top/
中新建 nav-top.less
樣式文件:.ant-menu { display: inline-block; float: right;}
隨后,在 同級(jí)的文件 nav-top.jsx
中引入 nav-top.less
樣式文件:...import { Menu } from 'antd';import "./nav-top.less" // 引入nav-top.less 樣式文件export default class NavTop extends Component{...
注: 為什么要在 src/assets/css/ 中新建一個(gè) index.less 樣式文件之后,還要在 nav-top.jsx 同級(jí)目錄中新建一個(gè) nav-top.less 樣式文件。關(guān)鍵詞:靜態(tài)
客戶&案例
營(yíng)銷資訊
關(guān)于我們
客戶&案例
營(yíng)銷資訊
關(guān)于我們
微信公眾號(hào)
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。