开始创建项目

开始创建项目

  1. 创建项目

      #创建nuxt项目
      #模版下载不下来,需要翻墙
      #先设置代理
      export http_proxy='http://127.0.0.1:10887'
      export https_proxy='http://127.0.0.1:10887'
      npm create nuxt <project-name>
      #模版可以下载下来了,但npm insatll 比较慢
      #可以先停止掉,然后不通过代理,而是直接运行,这样就会通过镜像来下载安装包了。
      npm install
      #开发运行
      npm run dev
  2. 安装bootstrap

      #安装bootstrap
      npm i bootstrap @popperjs/core
      #nuxt 中已经默认安装了sass,所以就不需要单独安装了。
      #⚠️在编译过程中报警告或者错误,需要降级sass 到 1.64.2 版本
      npm install sass@1.64.2
      #查看是否降级成功
      npm list sass
    
      #安装icons库
      npm i bootstrap-icons
  3. 使用bootstrap

      //非定制使用
      import "bootstrap/dist/css/bootstrap.min.css"
  4. 引入js

      //由于bootstrap 和@popperjs/core 都是客户端使用过的js库
      //所以不能直接引入,需要引入只在客户端使用
      //引入顺序是先引入@popperjs/core,在引入bootstrap
      //步骤在项目根目录建立:plugins 目录
      //新建 bootstrap.client.js 文件,文件名中的client代表只在客户端加载
      //文件内容如下:
      import * as popper from '@popperjs/core'
      import * as bootstrap from 'bootstrap';
    
    
      export default defineNuxtPlugin((nuxtApp) => {
          nuxtApp.provide('bootstrap', bootstrap) // 将库注入到 Nuxt 上下文
          nuxtApp.provide('popper', popper)
      })
    
      // 在组件中通过 useNuxtApp() 使用:
      {/* <script setup>
      const { $myLib } = useNuxtApp()
      onMounted(() => {
      $myLib.doSomething() // 安全使用客户端库
      })
      </script> */}
  5. 定制使用bootstrap

      #建立定制css文件,在assets/style.scss
      #根据自己的使用情况放开注释
      #定制主题颜色,具体位置是在大下方的包含下方两行内容的地方定制。 
      #// 定制 primary 颜色
      #$primary: #7952b3;
    
      // Override Bootstrap's Sass default variables
      //
      // Nearly all variables in Bootstrap are written with the `!default` flag.
      // This allows you to override the default values of those variables before
      // you import Bootstrap's source Sass files.
      //
      // Overriding the default variable values is the best way to customize your
      // CSS without writing _new_ styles. For example, you can either change
      // `$body-color` or write more CSS that override's Bootstrap's CSS like so:
      // `body { color: red; }`.
    
    
      //
      // Bring in Bootstrap
      //
    
      // Option 1
      //
      // Import all of Bootstrap's CSS
    
      // @import "bootstrap/scss/bootstrap";
    
      // Option 2
      //
      // Place variable overrides first, then import just the styles you need. Note that some stylesheets are required no matter what.
    
      // Toggle global options
      $enable-gradients: true;
      $enable-shadows: true;
    
      $offcanvas-box-shadow: 0 1rem 3rem rgba(0, 0, 0, .175);
    
      // Include functions first
      @import "bootstrap/scss/functions";
    
      // Customize some defaults
      // $body-color: #333;
      // $body-bg: #fff;
      // $border-radius: .4rem;
      // $success: #7952b3;
    
      // 定制 primary 颜色
      $primary: #7952b3;
    
      // Required
      @import "bootstrap/scss/variables";
      @import "bootstrap/scss/variables-dark";
      @import "bootstrap/scss/maps";
      @import "bootstrap/scss/mixins";
      @import "bootstrap/scss/utilities";
      @import "bootstrap/scss/root";
      @import "bootstrap/scss/reboot";
    
      @import "bootstrap/scss/type";
      // @import "bootstrap/scss/images";
      @import "bootstrap/scss/containers";
      @import "bootstrap/scss/grid";
      // @import "bootstrap/scss/tables";
      // @import "bootstrap/scss/forms";
      @import "bootstrap/scss/buttons";
      @import "bootstrap/scss/transitions";
      @import "bootstrap/scss/dropdown";
      // @import "bootstrap/scss/button-group";
      // @import "bootstrap/scss/nav";
      // @import "bootstrap/scss/navbar"; // Requires nav
      // @import "bootstrap/scss/card";
      // @import "bootstrap/scss/breadcrumb";
      // @import "bootstrap/scss/accordion";
      // @import "bootstrap/scss/pagination";
      // @import "bootstrap/scss/badge";
      // @import "bootstrap/scss/alert";
      // @import "bootstrap/scss/progress";
      // @import "bootstrap/scss/list-group";
      @import "bootstrap/scss/close";
      // @import "bootstrap/scss/toasts";
      @import "bootstrap/scss/modal"; // Requires transitions
      // @import "bootstrap/scss/tooltip";
      @import "bootstrap/scss/popover";
      // @import "bootstrap/scss/carousel";
      // @import "bootstrap/scss/spinners";
      @import "bootstrap/scss/offcanvas"; // Requires transitions
      // @import "bootstrap/scss/placeholders";
    
      // Helpers
      // @import "bootstrap/scss/helpers";
    
      // Utilities
      @import "bootstrap/scss/utilities/api";
  6. 使用bootstrap-icons

      //在app.vue中引入
      import "bootstrap-icons/font/bootstrap-icons.css"

# 开始创建项目

1. 创建项目
  ```bash
    #创建nuxt项目
    #模版下载不下来,需要翻墙
    #先设置代理
    export http_proxy='http://127.0.0.1:10887'
    export https_proxy='http://127.0.0.1:10887'
    npm create nuxt <project-name>
    #模版可以下载下来了,但npm insatll 比较慢
    #可以先停止掉,然后不通过代理,而是直接运行,这样就会通过镜像来下载安装包了。
    npm install
    #开发运行
    npm run dev
  ```
2. 安装bootstrap
  ```bash
    #安装bootstrap
    npm i bootstrap @popperjs/core
    #nuxt 中已经默认安装了sass,所以就不需要单独安装了。
    #⚠️在编译过程中报警告或者错误,需要降级sass 到 1.64.2 版本
    npm install sass@1.64.2
    #查看是否降级成功
    npm list sass

    #安装icons库
    npm i bootstrap-icons
  ```
3. 使用bootstrap
  ```javascript
    //非定制使用
    import "bootstrap/dist/css/bootstrap.min.css"
  ```
4. 引入js
  ```javascript
    //由于bootstrap 和@popperjs/core 都是客户端使用过的js库
    //所以不能直接引入,需要引入只在客户端使用
    //引入顺序是先引入@popperjs/core,在引入bootstrap
    //步骤在项目根目录建立:plugins 目录
    //新建 bootstrap.client.js 文件,文件名中的client代表只在客户端加载
    //文件内容如下:
    import * as popper from '@popperjs/core'
    import * as bootstrap from 'bootstrap';


    export default defineNuxtPlugin((nuxtApp) => {
        nuxtApp.provide('bootstrap', bootstrap) // 将库注入到 Nuxt 上下文
        nuxtApp.provide('popper', popper)
    })

    // 在组件中通过 useNuxtApp() 使用:
    {/* <script setup>
    const { $myLib } = useNuxtApp()
    onMounted(() => {
    $myLib.doSomething() // 安全使用客户端库
    })
    </script> */}
  ```
5. 定制使用bootstrap
  ```scss
    #建立定制css文件,在assets/style.scss
    #根据自己的使用情况放开注释
    #定制主题颜色,具体位置是在大下方的包含下方两行内容的地方定制。 
    #// 定制 primary 颜色
    #$primary: #7952b3;

    // Override Bootstrap's Sass default variables
    //
    // Nearly all variables in Bootstrap are written with the `!default` flag.
    // This allows you to override the default values of those variables before
    // you import Bootstrap's source Sass files.
    //
    // Overriding the default variable values is the best way to customize your
    // CSS without writing _new_ styles. For example, you can either change
    // `$body-color` or write more CSS that override's Bootstrap's CSS like so:
    // `body { color: red; }`.


    //
    // Bring in Bootstrap
    //

    // Option 1
    //
    // Import all of Bootstrap's CSS

    // @import "bootstrap/scss/bootstrap";

    // Option 2
    //
    // Place variable overrides first, then import just the styles you need. Note that some stylesheets are required no matter what.

    // Toggle global options
    $enable-gradients: true;
    $enable-shadows: true;

    $offcanvas-box-shadow: 0 1rem 3rem rgba(0, 0, 0, .175);

    // Include functions first
    @import "bootstrap/scss/functions";

    // Customize some defaults
    // $body-color: #333;
    // $body-bg: #fff;
    // $border-radius: .4rem;
    // $success: #7952b3;

    // 定制 primary 颜色
    $primary: #7952b3;

    // Required
    @import "bootstrap/scss/variables";
    @import "bootstrap/scss/variables-dark";
    @import "bootstrap/scss/maps";
    @import "bootstrap/scss/mixins";
    @import "bootstrap/scss/utilities";
    @import "bootstrap/scss/root";
    @import "bootstrap/scss/reboot";

    @import "bootstrap/scss/type";
    // @import "bootstrap/scss/images";
    @import "bootstrap/scss/containers";
    @import "bootstrap/scss/grid";
    // @import "bootstrap/scss/tables";
    // @import "bootstrap/scss/forms";
    @import "bootstrap/scss/buttons";
    @import "bootstrap/scss/transitions";
    @import "bootstrap/scss/dropdown";
    // @import "bootstrap/scss/button-group";
    // @import "bootstrap/scss/nav";
    // @import "bootstrap/scss/navbar"; // Requires nav
    // @import "bootstrap/scss/card";
    // @import "bootstrap/scss/breadcrumb";
    // @import "bootstrap/scss/accordion";
    // @import "bootstrap/scss/pagination";
    // @import "bootstrap/scss/badge";
    // @import "bootstrap/scss/alert";
    // @import "bootstrap/scss/progress";
    // @import "bootstrap/scss/list-group";
    @import "bootstrap/scss/close";
    // @import "bootstrap/scss/toasts";
    @import "bootstrap/scss/modal"; // Requires transitions
    // @import "bootstrap/scss/tooltip";
    @import "bootstrap/scss/popover";
    // @import "bootstrap/scss/carousel";
    // @import "bootstrap/scss/spinners";
    @import "bootstrap/scss/offcanvas"; // Requires transitions
    // @import "bootstrap/scss/placeholders";

    // Helpers
    // @import "bootstrap/scss/helpers";

    // Utilities
    @import "bootstrap/scss/utilities/api";
  ```
6. 使用bootstrap-icons
  ```javascript
    //在app.vue中引入
    import "bootstrap-icons/font/bootstrap-icons.css"
  ```