thinkphp6.0多应用安装Element UI的详细步骤
时间: 2023-12-04 08:00:07 浏览: 119
1. 下载Element UI
在官方网站https://element.eleme.cn/#/zh-CN/component/installation下载Element UI,解压到thinkphp6.0项目的public目录下。
2. 配置路由
在应用目录下的route目录中新建一个admin.php文件,用于配置后台路由。
代码如下:
```
<?php
// admin模块的路由
use think\facade\Route;
Route::rule('/', 'index/index');
Route::rule('login', 'login/index');
Route::rule('home', 'home/index');
Route::rule('user', 'user/index');
```
3. 创建控制器
在应用目录下的controller目录中新建一个Admin控制器,用于管理后台页面。
代码如下:
```
<?php
namespace app\admin\controller;
use think\Controller;
class Admin extends Controller
{
public function index()
{
return $this->fetch();
}
public function login()
{
return $this->fetch();
}
public function home()
{
return $this->fetch();
}
public function user()
{
return $this->fetch();
}
}
```
4. 创建视图文件
在应用目录下的view目录中新建一个admin目录,用于存放后台页面的视图文件。
代码如下:
index.html
```
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>后台首页</title>
<link rel="stylesheet" href="/element-ui/theme-chalk/index.css">
</head>
<body>
<div id="app">
<el-alert
title="成功提示"
type="success"
:closable="false"
show-icon>
</el-alert>
</div>
<script src="/element-ui/index.js"></script>
<script>
new Vue({
el: '#app'
});
</script>
</body>
</html>
```
login.html
```
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>后台登录</title>
<link rel="stylesheet" href="/element-ui/theme-chalk/index.css">
</head>
<body>
<div id="app">
<el-form>
<el-form-item label="用户名">
<el-input v-model="form.username"></el-input>
</el-form-item>
<el-form-item label="密码">
<el-input type="password" v-model="form.password"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm">登录</el-button>
</el-form-item>
</el-form>
</div>
<script src="/element-ui/index.js"></script>
<script>
new Vue({
el: '#app',
data: {
form: {
username: '',
password: ''
}
},
methods: {
submitForm() {
console.log(this.form);
}
}
});
</script>
</body>
</html>
```
home.html
```
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>后台首页</title>
<link rel="stylesheet" href="/element-ui/theme-chalk/index.css">
</head>
<body>
<div id="app">
<el-menu :default-active="activeIndex" mode="horizontal" @select="handleSelect">
<el-menu-item index="1">菜单1</el-menu-item>
<el-menu-item index="2">菜单2</el-menu-item>
<el-menu-item index="3">菜单3</el-menu-item>
</el-menu>
</div>
<script src="/element-ui/index.js"></script>
<script>
new Vue({
el: '#app',
data: {
activeIndex: '1'
},
methods: {
handleSelect(index, indexPath) {
console.log(index, indexPath);
}
}
});
</script>
</body>
</html>
```
user.html
```
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户管理</title>
<link rel="stylesheet" href="/element-ui/theme-chalk/index.css">
</head>
<body>
<div id="app">
<el-table :data="tableData" style="width: 100%">
<el-table-column prop="name" label="姓名"></el-table-column>
<el-table-column prop="age" label="年龄"></el-table-column>
<el-table-column prop="address" label="地址"></el-table-column>
</el-table>
</div>
<script src="/element-ui/index.js"></script>
<script>
new Vue({
el: '#app',
data: {
tableData: [{
name: '张三',
age: 18,
address: '北京市朝阳区'
}, {
name: '李四',
age: 20,
address: '上海市浦东新区'
}, {
name: '王五',
age: 22,
address: '广州市天河区'
}]
}
});
</script>
</body>
</html>
```
5. 访问后台页面
访问http://localhost/admin/index即可看到后台首页。其他页面同理。
阅读全文