Hướng dẫn cài đặt và sử dụng thư viện Elementjs nuxtjs

folder Kiến thức công nghệ
today 28/11/2024 23:14
Thư viện Element UI là một bộ thư viện giao diện người dùng mạnh mẽ, cung cấp nhiều thành phần (components) tùy chỉnh sẵn, giúp bạn xây dựng giao diện ứng dụng dễ dàng và nhanh chóng. Đối với Nuxt.js , bạn có thể tích hợp và sử dụng Element UI một cách đơn giản. Dưới đây là hướng dẫn từng bước.
huong-dan-cai-dat-va-su-dung-thu-vien-elementjs-nuxtjs

Cài đặt và sử dụng Element

1. Tạo Dự Án Nuxt.js Mới

                 Nếu bạn chưa có dự án Nuxt.js, bạn có thể tạo một dự án mới :

Chú ý: Có 2 cách để tạo dự án Nuxtjs đó là:

  • npm
  • yarn

Đối với yarn chạy lệnh sau :

yarn create nuxt-app <project-name>

Còn đối với npm thì  tạo dự án bằng cách sử dụng npx:

npx create-nuxt-app servbay-nuxt-app

2. Cài đặt Element Plus

Đầu tiên, bạn cần cài đặt thư viện Element Plus và các gói liên quan.

Mở terminal và chạy lệnh:

npm install element-plus

Hoặc sử dụng yarn:

yarn add element-plus

Nếu bạn muốn tích hợp Element Plus Icons, cài đặt thêm:

npm install @element-plus/icons-vue

3. Cấu hình Element Plus trong dự án Nuxt.js

Tự động import Element Plus

  1. Cài đặt plugin unplugin-element-plus:

    npm install -D unplugin-element-plus
  2. Mở file nuxt.config.ts và thêm plugin:

    import ElementPlus from 'unplugin-element-plus/vite';
    
    export default defineNuxtConfig({
      modules: [],
      vite: {
        plugins: [
          ElementPlus({ /* Tùy chỉnh nếu cần */ })
        ]
      }
    });
    

Sử dụng thủ công Element Plus

Nếu không muốn dùng plugin, bạn có thể import Element Plus thủ công:

  1. Tạo file plugin tại plugins/element-plus.ts:

    import { defineNuxtPlugin } from '#app';
    import ElementPlus from 'element-plus';
    import 'element-plus/dist/index.css';
    
    export default defineNuxtPlugin((nuxtApp) => {
      nuxtApp.vueApp.use(ElementPlus);
    });
    
  2. Sau đó, Nuxt.js sẽ tự động load plugin này.


4. Sử dụng Element Plus trong component

Bây giờ bạn có thể sử dụng các component của Element Plus trong dự án.

Ví dụ:

<template>
  <div>
    <el-button type="primary">Primary Button</el-button>
    <el-date-picker v-model="date" type="date" placeholder="Select Date"></el-date-picker>
  </div>
</template>

<script setup>
import { ref } from 'vue';

const date = ref(null);
</script>

5. Tùy chỉnh theme

Element Plus cho phép bạn tùy chỉnh giao diện bằng cách sử dụng CSS biến (CSS Variables).

  1. Tạo file assets/styles/element-plus.scss:

:root {
  --el-color-primary: #42b983; // Màu chủ đạo
  --el-font-size-base: 16px;  // Kích thước font
}

       2.Import file này vào nuxt.config.ts:

export default defineNuxtConfig({
  css: ['@/assets/styles/element-plus.scss']
});

6. Cài đặt và sử dụng icon

Khi sử dụng @element-plus/icons-vue, bạn cần import icon tương ứng:

Ví dụ:

<template>
  <div>
    <el-icon><Edit /></el-icon>
    <el-button icon="Edit">Edit</el-button>
  </div>
</template>

<script setup>
import { Edit } from '@element-plus/icons-vue';
</script>

7. Một số lưu ý khi sử dụng với Nuxt.js

  • Element Plus hoạt động tốt với Nuxt 3 nhờ hỗ trợ Vue 3. Hãy đảm bảo bạn đang sử dụng phiên bản mới nhất của Nuxt.
  • Khi cần render phía server (SSR), Element Plus hoạt động ổn định, nhưng nên kiểm tra kỹ nếu có lỗi liên quan đến Hydration.

Bài viết cùng chuyên mục

hatonet

Hướng dẫn cài và sử dụng vue-object-to-formdata cho dự án nuxtjs

today 11/12/2024 22:15
vue-object-to-formdata là một thư viện JavaScript được thiết kế để chuyển đổi một đối tượng JavaScript (Object) thành đối tượng FormData. Đây là một đối tượng có thể dễ dàng sử dụng trong các yêu cầu HTTP để gửi dữ liệu, đặc biệt là khi bạn cần gửi dữ liệu như là một phần của biểu mẫu (form), ví dụ: khi upload file hoặc gửi dữ liệu dưới dạng multipart/form-data.
Xem thêm arrow_right_alt
hatonet

Hướng dẫn cài đặt và sử dụng v-tooltip trong project nuxt js

today 04/12/2024 21:34
v-tooltip là một thư viện Vue.js nhẹ, dễ sử dụng, giúp tạo ra các tooltip (hộp thông báo khi người dùng di chuột qua một phần tử) trong các ứng dụng Vue. Tooltip là một yếu tố giao diện người dùng (UI) thông báo hoặc giải thích ngắn gọn về một đối tượng khi người dùng hover (di chuột) hoặc focus vào phần tử đó.
Xem thêm arrow_right_alt
hatonet

Hướng dẫn cài đặt và sử dụng Buefy - thư viện UI nhẹ dành cho Vue.js

today 28/11/2024 22:41
Buefy là một thư viện UI nhẹ dành cho Vue.js, được xây dựng trên Bulma CSS. Nó cung cấp các thành phần giao diện đẹp, dễ tùy chỉnh và thân thiện với người dùng. Với thiết kế đơn giản nhưng mạnh mẽ, Buefy rất phù hợp cho các dự án cần sự linh hoạt trong giao diện mà không làm tăng kích thước gói cài đặt.
Xem thêm arrow_right_alt

Việc làm tại Hatonet

- ECパッケージシステムのフロントエンド(React)開発に参加。 - 主に新機能の開発を担当し、あわせてバグ修正やUI/UXの改善も行いました。

  • ReactJS
Remote
1. Technology stack: ● Front-End: Angular (primary framework), PrimeNG (UI widget library), and Tailwind CSS (styling framework). ● The developer must ensure seamless integration of the UI with the existing back-end infrastructure. 2. Project timeline: ● The foundational technology has been validated, and the current focus is on building the administrative UI layer, requiring significant front-end development. ● A soft launch is scheduled for September 2025, described as a market introduction to collect feedback rather than a full commercial release. ● The project is initially short-term, with potential for scaling based on market feedback and customer interest (some customers have already expressed interest). 3. Administrative requirements: ● A Non-Disclosure Agreement (NDA) is required to protect confidential project information. NKKTECH will provide an NDA template. ● The developer must pass a background check (e.g., verification of no criminal record, potentially via a US service like E-Verify) 4. Work environment: ● Remote: (14h-18h and 20h-24h) 5. Selection process: 2 Rounds • Round 1: Will conduct an interview to assess English proficiency and technical skills. • Round 2: The clientwill conduct interviews, which may involve discussing code samples to evaluate technical skills. Live coding tests are not confirmed but possible. • The selected candidate must pass a background check and sign an NDA before onboarding.

  • AngularJS
Remote