本章修改的内容比较多,因为牵扯了比较多的前端样式,可以运行的代码在 大家可以对照查漏补缺。

前端优化

首先我们安装一下依赖,transitions 里面是动画指令,extras 里面是帮助方法,比如数组的 push。

  1. npm install --save svelte-transitions svelte-extras

修改 package.json,添加打包白名单。

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>App</title>
  8. <style>
  9. html {
  10. background: #fff;
  11. -webkit-app-region: drag
  12. }
  13. * {
  14. font-family: "PingFang SC";
  15. margin: 0;
  16. padding: 0;
  17. box-sizing: border-box;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. </body>

添加全局消息组件

新建 compoennts/Tip.svelte,这个用于全局消息提示。

添加好这些方法,当然别忘记在 store 里面初始化 msg,这些都是提示消息的帮助方法。

  1. function setMsg(type, content) {
  2. store.set({
  3. msg: {
  4. type,
  5. content
  6. }
  7. })
  8. }
  9. function resetMsg() {
  10. store.set({
  11. msg: {
  12. type: 'info',
  13. content: ''
  14. }
  15. })
  16. }
  17. function success(content, timer = 1000) {
  18. store.set({
  19. msg: {
  20. type: 'success',
  21. content
  22. }
  23. })
  24. setTimeout(resetMsg, timer)
  25. }
  26. store.success = success.bind(store)

修改 App.svelte

就像手机端回退的按钮一样,新建 components/Back.svelte,这里的按钮我们通过 css 画出来。

  1. <Link className="back" to="Main"><i class="back-icon"></i></Link>
  2. <script>
  3. export default {
  4. components: {
  5. Link: './Link.svelte'
  6. }
  7. }
  8. </script>
  9. <style>
  10. .back-icon {
  11. border-left: 4px solid #fff;
  12. border-bottom: 4px solid #fff;
  13. display: inline-block;
  14. width: 15px;
  15. height: 15px;
  16. border-radius: 2px;
  17. transform: rotate(45deg);
  18. margin: 8px 8px 8px 0.8rem;
  19. }
  20. :global(.back) {
  21. width: 100%;
  22. height: 60px;
  23. background: #383A41;
  24. display: flex;
  25. align-items: center;
  26. }