• .Net Core Vue Qucik Start


    .Net Core Vue Qucik Start

    This is a ASP.NET Core 3.0 project seamlessly integrationed with Vue.js template.

    A complaint from Microsoft officials:

    As far as I'm aware, we don't have plans to introduce Vue-specific features. This isn't because we have anything against Vue, but rather just to limit the growth in the number of frameworks that we're maintaining support for. The dev team only has a finite capacity for handling third-party concepts, and last year we made the strategic choice to focus on only Angular and React.

    Microsoft won't stop our enthusiasm for vuejs

    The Microsoft's dev team only has a finite capacity for handling third-party concepts, but we chinese men don't. Men can never say no.

    Let's Set Sail

    1. Create a new project with react template

    • You can use Visual Studio to create a project with React.js:

    step1.1.jpg

    • Or execute dotnet new react command in Command Line Tools:

    step1.2.jpg

    2. Change Reactjs template to Vuejs template

    • Remove ClientApp folder:

    step2.1.jpg

    step2.2.jpg

    • Create new vue template project in root folder:

    step3.1.jpg

    step3.2.jpg

    • Rename all ClientApp folder to our vue project name:

    Startup.cs

        public void ConfigureServices(IServiceCollection services)
        {
            ...
    
            services.AddSpaStaticFiles(configuration =>
            {
                // configuration.RootPath = "ClientApp/build";
                configuration.RootPath = "admin/build";
            });
        }
    
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            ...
    
            app.UseSpa(spa =>
            {
                // spa.Options.SourcePath = "ClientApp";
                spa.Options.SourcePath = "admin";
    
                ...
            });
        }
    

    NetCoreVue.csproj

      <PropertyGroup>
        <TargetFramework>netcoreapp3.0</TargetFramework>
        <TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
        <TypeScriptToolsVersion>Latest</TypeScriptToolsVersion>
        <IsPackable>false</IsPackable>
        <!-- <SpaRoot>ClientApp</SpaRoot> -->
        <SpaRoot>admin</SpaRoot>
        <DefaultItemExcludes>$(DefaultItemExcludes);$(SpaRoot)node_modules**</DefaultItemExcludes>
      </PropertyGroup>
    
    • Add VueCliMiddleware package from nuget:

    Run dotnet add package VueCliMiddleware command in the Package Manager Console.

    step3.3.jpg

    • Change ReactDevelopmentServer to VueCli:
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            ...  
    
            app.UseSpa(spa =>
            {
                spa.Options.SourcePath = "admin";
    
                if (env.IsDevelopment())
                {
                    // spa.UseReactDevelopmentServer(npmScript: "start");
                    spa.UseVueCli();
                }
            });
        }
    
    • Change React build floder 'build' to Vue build folder 'dist':

    Startup.cs

        public void ConfigureServices(IServiceCollection services)
        {
            ...
    
            services.AddSpaStaticFiles(configuration =>
            {
                // configuration.RootPath = "admin/build";
                configuration.RootPath = "admin/dist";
            });
        }
    

    NetCoreVue.csproj

        <ItemGroup>
          <!-- <DistFiles Include="$(SpaRoot)build**" /> -->
          <DistFiles Include="$(SpaRoot)dist**" />
          <ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
            <RelativePath>%(DistFiles.Identity)</RelativePath>
            <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
            <ExcludeFromSingleFile>true</ExcludeFromSingleFile>
          </ResolvedFileToPublish>
        </ItemGroup>
    
    • Run to test

    Run dotnet run in Command Line Tools to run the app.

    step3.4.jpg

    3. Case will be in the end

    • Install axios plugin:

    Run vue add axios command in Command Line Tools to install axios.

    step4.1.jpg

    • Run vue add router command in Command Line Tools to install vue-router.

    step4.2.jpg

    • add WeatherForecast.vue in views folder:
    <template>
        <div class="weather">
            <table className='table table-striped' aria-labelledby="tabelLabel">
                <thead>
                    <tr>
                        <th>Date</th>
                        <th>Temp. (C)</th>
                        <th>Temp. (F)</th>
                        <th>Summary</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="(forecast,index) in forecasts" :key="forecast.date">
                        <td>{{forecast.date}}</td>
                        <td>{{forecast.temperatureC}}</td>
                        <td>{{forecast.temperatureF}}</td>
                        <td>{{forecast.summary}}</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </template>
    
    <script>
        export default {
            name: 'WeatherForecast',
            data() {
                return {
                    forecasts:[]
                };
            },
            created() {
                this.axios.get("/weatherforecast").then(res => {
                    // console.log(res.data);
                    this.forecasts = res.data;
                });
            }
        }
    </script>
    
    <!-- Add "scoped" attribute to limit CSS to this component only -->
    <style scoped>
    
        body{
            text-align:center;
        }
    
        .weather {
            margin: 0 auto;
        }
    </style>
    
    • Add a new router:
    export default new Router({
      mode: 'history',
      base: process.env.BASE_URL,
      routes: [
        ...
        {
            path: '/weather',
            name: 'weather',
            component: () => import('./views/WeatherForecast.vue')
        }
      ]
    })
    
    • Run to view the last result:

    step5.jpg

    Enjoy it!

  • 相关阅读:
    【转发】mysql数据库遇到的故障及分析_连接MySQL数据库时常见故障问题的分析与解决
    ubuntu php 上传文件临时目录,PHP $_FILES 上传文件,在Windows下正常,但在Ubuntu下无法上传文件
    hive从入门到放弃(六)——常用文件存储格式
    手把手教你使用Git管理你的软件代码
    python剪切视频
    分享一个 SpringCloud Feign 中所埋藏的坑
    【转】wireshark分析RDP数据前的设置
    pyshark报错: lxml.etree.XMLSyntaxError: Input is not proper UTF8, indicate encoding !
    vim 配色
    协议层安全相关《http请求走私与CTF利用》
  • 原文地址:https://www.cnblogs.com/Run2948/p/11845778.html
Copyright © 2020-2023  润新知