vue新手第一次尝试将tp5和vue结合起来,特此记录以备自己回顾,供跟我一样在摸索中前行的程序猿们参考;如果有不合理的地方,欢迎大神们指正!

php中文网的“vue+tp分离开发实时疫情动态地图”视频教程https://www.php.cn/course/1172.html 对这次学习有很大的指导作用,下面的记录中也有用到教程中的课程源码

一、TP5准备后台数据

1.application目录下新建six_products模块,mytestt控制器,index方法

这个方法通过外部API接口获取国外疫情数据,筛选出有用的部分,返回json格式的数据

  1. public function index()
  2. {
  3. //通过外部接口获取国外疫情数据
  4. $ex_res1 = file_get_contents("http://api.tianapi.com/txapi/ncovabroad/index?key=6c6d319a9f3b53c53b375c56fbd39207");
  5. $ex_res = json_decode($ex_res1, true);
  6. $list = $ex_res['newslist'];
  7. $newList = [];
  8. if ($list != null) {
  9. foreach ($list as $k => $v) {
  10. $newList[$k]['name'] = $v['provinceName'];//国家名字
  11. $newList[$k]['currentConfirmedCount'] = $v['currentConfirmedCount'];//当前确诊人数
  12. $newList[$k]['confirmedCount'] = $v['confirmedCount'];//累计确诊人数
  13. $newList[$k]['deadCount'] = $v['deadCount'];//死亡人数
  14. $newList[$k]['curedCount'] = $v['curedCount'];//治愈人数
  15. }
  16. }
  17. //返回json数据
  18. return json($newList);
  19. }

二.后端跨域路由配置

1.找到TP5的route文件夹下的路由配置文件route.php,并打开

2.添加以下代码,配置跨域路由

  1. //后端跨域路由配置
  2. Route::get('mytestt/index','six_products/mytestt/index')->allowCrossDomain();

三、前端通过AJAX(AXIOS)获取后端数据

1.引入axios,注册axios变量和后端域名变量

在前端的main.js文件中添加以下代码

  1. import axios from 'axios'
  2. // 注册axios变量
  3. Vue.prototype.$axios = axios
  4. // 注册后端域名变量
  5. Vue.prototype.$host = 'https://test.cn/public/'

其中https://test.cn/public是后端的域名

2.通过axios读取数据,并显示出来(在前端的App.vue的<script></script>标签中填写以下代码,完整代码在末尾)

  1. created () {
  2. this.$axios.get(this.$host + 'mytestt/index').then(res => {
  3. const data = res.data
  4. this.curedCount = data[0].curedCount
  5. this.deadCount = data[0].deadCount
  6. this.currentConfirmedCount = data[0].currentConfirmedCount
  7. this.confirmedCount = data[0].confirmedCount
  8. this.update_time = data.update_time
  9. this.country = data[0].name
  10. })
  11. .catch(res => {
  12. console.log(res)
  13. })
  14. },

上面代码中$axios.get()中的’mytestt/index’就是第二步设置的后端跨域路由地址,两个一定要正确对应起来

四、打包vue项目

在前端项目的terminal窗口执行cnpm run build命令,打包完成后会在项目根目录下生成一个dist文件夹,包含static文件下和index.html文件,

五、将打包好的vue前端文件放到tp5的项目中

(我放到了控制器的index方法中,据说也可以直接放到public目录下,具体参考:https://blog.csdn.net/qq236710052/article/details/107592387)

5.1 放Index.html文件

5.1.1 在TP5的application目录six_products模块下,新建mytestu控制器,index方法
  1. <?php
  2. namespace app\six_products\controller;
  3. use think\Controller;
  4. class Mytestu extends Controller
  5. {
  6. public function index(){
  7. return $this->view->fetch();
  8. }
  9. }
5.1.2 在six_products模块下的view目录下新建mytestu文件夹,将前端打包好的dist文件夹下的index.html文件,直接放到刚才新建的mytestu文件夹下

5.2 放css和js文件,将css和js文件放在tp5项目的public/static/mytestu文件夹下

5.2.1 直接将前端打包好的dist文件夹下的css和js文件夹复制粘贴到public/static/mytestu文件夹下

5.3 修改前端index文件中的(six_products/view/mytestu/index.html)css和js文件引入路径

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset=utf-8>
  5. <meta name=viewport content="width=device-width,initial-scale=1">
  6. <title>my-project</title>
  7. <link href=/public/static/mytestu/css/app.9d739eebe64572b1f681feaa09be9e07.css rel=stylesheet>
  8. </head>
  9. <body>
  10. <div id=app></div>
  11. <script type=text/javascript src=/public/static/mytestu/js/manifest.2ae2e69a05c33dfc65f8.js></script>
  12. <script type=text/javascript src=/public/static/mytestu/js/vendor.717a4ce3893d952882a7.js></script>
  13. <script type=text/javascript src=/public/static/mytestu/js/app.5230369e4a91166d76b4.js></script>
  14. </body>
  15. </html>

这样就完成了整个项目的搭建,在浏览器中输入
https://test.cn/public/six_products/mytestu/index访问的结果如下图

在此附上前端完整的App.vue文件代码:

  1. <template>
  2. <div id="app">
  3. <h3>{{country}}疫情</h3>
  4. <p class="txt">来源:各地官方通报及权威媒体报道</p>
  5. <p class="txt">更新:{{update_time}}</p>
  6. <div class="flexbox">
  7. <div class="item">
  8. <div class="red bold">{{confirmedCount}}</div>
  9. <div class="gray tittle">累计确诊</div>
  10. </div>
  11. <div class="item">
  12. <div class="red bold">{{currentConfirmedCount}}</div>
  13. <div class="gray tittle">现确诊</div>
  14. </div>
  15. <div class="item">
  16. <div class="gray bold">{{deadCount}}</div>
  17. <div class="gray tittle">死亡
  18. </div>
  19. </div>
  20. <div class="item">
  21. <div class="green bold">{{curedCount}}</div>
  22. <div class="gray tittle">治愈</div>
  23. </div>
  24. </div>
  25. </div>
  26. </template>
  27. <script>
  28. export default {
  29. name: 'App',
  30. data () {
  31. return {
  32. country: '美国',
  33. update_time: '',
  34. confirmedCount: 0,
  35. currentConfirmedCount: 0,
  36. deadCount: 0,
  37. curedCount: 0
  38. }
  39. },
  40. created () {
  41. this.$axios.get(this.$host + 'mytestt/index').then(res => {
  42. const data = res.data
  43. this.curedCount = data[0].curedCount
  44. this.deadCount = data[0].deadCount
  45. this.currentConfirmedCount = data[0].currentConfirmedCount
  46. this.confirmedCount = data[0].confirmedCount
  47. this.update_time = data.update_time
  48. this.country = data[0].name
  49. })
  50. .catch(res => {
  51. console.log(res)
  52. })
  53. },
  54. methods: {
  55. }
  56. }
  57. </script>
  58. <style>
  59. #app {
  60. font-family: 'Avenir', Helvetica, Arial, sans-serif;
  61. -webkit-font-smoothing: antialiased;
  62. -moz-osx-font-smoothing: grayscale;
  63. text-align: center;
  64. color: #2c3e50;
  65. margin-top: 60px;
  66. }
  67. </style>
  68. <style scoped>
  69. .txt {
  70. font-size: 14px;
  71. color: grey;
  72. }
  73. .flexbox {
  74. margin-bottom: 10px;
  75. width: 100%;
  76. height: 80px;
  77. background-color: lightgray;
  78. border-radius: 5px;
  79. display: flex;
  80. justify-content: center;
  81. align-items: center;
  82. }
  83. .item {
  84. flex-direction: column;
  85. display: flex;
  86. width: 80px;
  87. height: 80px;
  88. margin: 0 50px;
  89. /* border:1px solid red; */
  90. justify-content: center;
  91. align-items: center;
  92. }
  93. .red {
  94. color: #cc0000
  95. }
  96. .green {
  97. color: #42B983;
  98. }
  99. .gray {
  100. color: gray
  101. }
  102. .tittle {
  103. font-size: 14px;
  104. }
  105. .bold {
  106. font-weight: 600;
  107. font-size: 18px;
  108. }
  109. </style>

更多相关文章

  1. 纯静态文件打包部署预览链接,来自于vue的cli官方文件,使用的是yarn
  2. ROS2中使用Gtes示例
  3. NPM包管理工具、webpack模块打包器体验
  4. java获取文件路径
  5. UNIX传统文件系统s5fs的实现
  6. php生成PDF文件的方法
  7. PHP基础:引入命名空间、文件上传和分页
  8. Spring Boot 实现配置文件加解密原理
  9. 如何在 Linux 下快速找到被删除的文件?

随机推荐

  1. JAVA面向对象基础
  2. JAVA生成带LOGO的二维码
  3. Java 编程下的同步代码块(售票员)
  4. 如何指定休眠连接映射?
  5. 在扑克游戏中显示卡片的图像
  6. JavaFX窗口自适应
  7. JAVA实现二进制和16进制之间的互相转换,8
  8. Java学习之面向对象三
  9. java微信小程序解密AES/CBC/PKCS7Padding
  10. 包含带标记的值的XML属性文件