App.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <template>
  2. <div id="app">
  3. <h2>路由</h2>
  4. <!-- 跳转路由标签 => a标签 -->
  5. <h3>跳转标签</h3>
  6. <div class="nav">
  7. <!-- params传参 -->
  8. <!-- <router-link active-class="active" :to="{
  9. name:'detail',
  10. params: {
  11. name1:'Lucy',
  12. age1: 10
  13. }
  14. }">去详情</router-link> -->
  15. <router-link active-class="active" to="/detail/小A/10" >去详情</router-link>
  16. <router-link active-class="active" to="/home">去首页</router-link>
  17. <!-- <router-link active-class="active" :to="{name:'home'}"
  18. >去首页</router-link
  19. > -->
  20. <!-- query传参 -->
  21. <!-- <router-link active-class="active" :to="{
  22. path:'/list',
  23. query:{
  24. names: '小郑',
  25. age: 30
  26. }
  27. }" >去列表</router-link> -->
  28. <router-link active-class="active" :to="`/list?names=小郑&age=30`" >去列表</router-link>
  29. </div>
  30. <!-- 占位显示 -->
  31. <h3>显示位置</h3>
  32. <div class="main">
  33. <router-view></router-view>
  34. </div>
  35. <hr />
  36. <h2>Vuex</h2>
  37. <!--
  38. State:
  39. 1.this.$store.state.xxx 在生命周期,或者适当的方法中
  40. 2.$store.state.xxx 在template模板中
  41. 3.引入mapState 在computed中 ...mapState(['xxx'])
  42. -->
  43. <p>1.名字{{ name1 }},来自{{ address1 }}</p>
  44. <p>2.名字{{ $store.state.name }},来自{{ $store.state.address }}</p>
  45. <p>3.名字{{ name }},来自{{ address }}</p>
  46. <hr />
  47. <!--
  48. Getters
  49. 1.this.$store.getters.xxx 在生命周期,或者适当的方法中
  50. 2.$store.getters.xxx 在template模板中
  51. 3.引入mapGetters 在computed中 ...mapGetters(['xxx']
  52. -->
  53. <p>1.数值:{{ count1 }}</p>
  54. <p>2.数值:{{ $store.getters.newCount }}</p>
  55. <p>2.数值:{{ newCount }}</p>
  56. <hr />
  57. <!--
  58. Mutations 同步
  59. 1.this.$store.commit("对应的方法",传参)
  60. 2.
  61. 引入mapMutations
  62. 解构 ...mapMutations(['xxx'],['xxx']....)
  63. 使用:this.xxx()
  64. -->
  65. <!--
  66. Actions 异步
  67. 定义:
  68. 方法名(context) {
  69. context.commit("调用的同步方法")
  70. }
  71. 1.this.$store.commit("对应的方法")
  72. 2.
  73. 引入mapActions
  74. 解构 ...mapActions(['xxx'],['xxx']....)
  75. 使用:this.xxx()
  76. -->
  77. <button @click="add">添加</button>
  78. <br /><br />
  79. <button @click="reduce">递减</button>
  80. <h2>组件库</h2>
  81. <el-button type="primary">主要按钮</el-button>
  82. <el-carousel indicator-position="outside" :autoplay="false">
  83. <el-carousel-item v-for="(item, index) in bannerList" :key="index">
  84. <!-- <h3>{{ item.imgUrl }}</h3> -->
  85. <img :src="item.imgUrl" alt="" />
  86. </el-carousel-item>
  87. </el-carousel>
  88. <hr />
  89. <el-table :data="tableData" style="width: 80%">
  90. <el-table-column prop="prodName" label="商品名称"> </el-table-column>
  91. <el-table-column prop="price" label="商品价格" width="180">
  92. </el-table-column>
  93. <el-table-column prop="brief" label="商品描述"> </el-table-column>
  94. </el-table>
  95. </div>
  96. </template>
  97. <script>
  98. import axios from "axios";
  99. // import {mapMutations} from 'vuex';
  100. import { mapState, mapGetters, mapActions } from "vuex";
  101. export default {
  102. data() {
  103. return {
  104. bannerList: [],
  105. tableData: [],
  106. name1: "",
  107. address1: "",
  108. count1: null,
  109. };
  110. },
  111. created() {
  112. this.init();
  113. console.log(this.$store, "222");
  114. this.name1 = this.$store.state.name;
  115. this.address1 = this.$store.state.address;
  116. this.count1 = this.$store.getters.newCount;
  117. },
  118. computed: {
  119. ...mapState(["name", "address"]),
  120. ...mapGetters(["newCount"]),
  121. },
  122. methods: {
  123. init() {
  124. // 走马灯
  125. axios
  126. .get("http://shop-api.edu.koobietech.com/indexImgs")
  127. .then((res) => {
  128. this.bannerList = res.data.data;
  129. })
  130. .catch((err) => {
  131. console.log(err, "失败");
  132. });
  133. // 表格
  134. axios
  135. .get("http://shop-api.edu.koobietech.com/prod/tagProdList")
  136. .then((res) => {
  137. this.tableData = res.data.data[0].productDtoList;
  138. console.log(this.tableData, "res");
  139. })
  140. .catch((err) => {
  141. console.log(err, "失败");
  142. });
  143. },
  144. add() {
  145. this.$store.commit("addCount", 10);
  146. },
  147. // 同步
  148. // ...mapMutations(['reduceCount']),
  149. // 异步
  150. ...mapActions(["asyncReduce"]),
  151. reduce() {
  152. // 同步
  153. // this.reduceCount();
  154. // this.$store.commit("reduceCount");
  155. // 异步
  156. this.$store.dispatch("asyncReduce");
  157. // this.asyncReduce()
  158. },
  159. },
  160. };
  161. </script>
  162. <style lang="scss" scoped>
  163. .el-carousel__item h3 {
  164. color: #475669;
  165. font-size: 18px;
  166. opacity: 0.75;
  167. line-height: 300px;
  168. margin: 0;
  169. }
  170. .el-carousel__item:nth-child(2n) {
  171. background-color: #99a9bf;
  172. }
  173. .el-carousel__item:nth-child(2n + 1) {
  174. background-color: #d3dce6;
  175. }
  176. .active {
  177. color: red;
  178. padding: 20px;
  179. border: 1px solid #000;
  180. }
  181. .nav {
  182. width: 500px;
  183. height: 100px;
  184. border: 1px solid plum;
  185. display: flex;
  186. justify-content: space-around;
  187. align-items: center;
  188. }
  189. .main {
  190. width: 800px;
  191. height: 800px;
  192. border: 1px solid yellowgreen;
  193. }
  194. </style>