Partager via


Use Fluent UI Web Components with Vue

Vue is fun to use and Fluent UI Web Components work great with it. Let's take a look at how you can set up a Vue project, starting from scratch.

Setting up the Vue project

First, you'll need to make sure that you have Node.js installed. You can learn more and download that on the official site.

With Node.js installed, you can run the following command to install the Vue CLI:

npm install -g @vue/cli

With the CLI installed, you have access to the vue command-line interface. This can be used to create a new Vue project. For example, to create a new Vue App named "fluent-vue", you would use the following command:

vue create fluent-vue

When prompted to select options, choose "Manually select features". Follow the prompts, answering each question in turn. It is recommended that you select "TypeScript" when prompted.

When the CLI completes, you should have a basic runnable Vue application.

Configuring packages

Next, we'll install the Fluent UI Web Component packages, along with supporting libraries. To do that, run this command from your new project folder:

npm install --save @fluentui/web-components @microsoft/fast-element lodash-es

Using the components

With all the basic pieces in place, let's run our app in dev mode with npm run serve. The Vue CLI should build your project and make it available on localhost. Right now, it displays a basic welcome message, since we haven't added any code or interesting HTML. Let's change that.

First, open your src/main.ts file and add the following code:

import { provideFluentDesignSystem, fluentCard, fluentButton } from '@fluentui/web-components';

provideFluentDesignSystem().register(fluentCard(), fluentButton());

This code uses the Fluent Design System to register the <fluent-card> and <fluent-button> components. Once you save, the dev server will rebuild and refresh your browser. However, you still won't see anything. To get some UI showing up, we need to write some HTML that uses our components. Replace the HTML template in your components/HelloWorld.vue file with the following markup:

<template>
  <fluent-card>
    <h2>{{msg}}</h2>
    <fluent-button appearance="accent" v-on:click="onClick">Click Me</fluent-button>
  </fluent-card>
</template>

Replace your script tag with this:

<script>
  export default {
    name: 'HelloWorld',
    props: {
      msg: String,
    },
    methods: {
      onClick: function () {
        console.log('clicked!');
      },
    },
  };
</script>

To add a splash of style, replace the style tag with this:

<style scoped>
  fluent-card {
    padding: 16px;
    display: flex;
    flex-direction: column;
  }

  h2 {
    font-size: var(--type-ramp-plus-5-font-size);
    line-height: var(--type-ramp-plus-5-line-height);
  }

  fluent-card > fluent-button {
    align-self: flex-end;
  }
</style>

You're now set up to use Fluent UI Web Components and Vue!

Next steps