Blogs

Using single-file components in a Vue.js portlet

Extending Liferay's npm-vuejs-portlet

A few weeks ago I started Maximilian Schwarzmüller's Udemy training Vue JS 2 - The Complete Guide, a highly recommend training for anyone who wants to learn Vue.js.

While I was doing the training I was testing the examples immediately in a Liferay portlet, because my goal for learning Vue.js is to use it as a front-end technology in Liferay.
 

Creating the Vue.js portlet

The base for this Liferay portlet I generated using Liferay's blade cli tool and the npm Vue.js portlet template:

blade init liferay-vuejs-examples
cd liferay-vuejs-examples/
blade create -t npm-vuejs-portlet triberay-vue-portlet

This results in a bundle with the following structure:

As you can see there is just one javascript file in the lib folder. This file contains all the Vue.js javascript code.
This was totally fine for the first sections of the Udemy training, which were just very basic Vue.js scripts.

SVG Graph Example

At this point, I also tried out some of the examples from the Vue.js website, like this SVG Graph example. I just copied the JavaScript code from the example in the portlet's index.es.js file, as well as the html code in the portlet's view.jsp (but making sure to still use the <portlet:namespace />-1 id instead of the demo id from the example).
This also worked perfectly so far.

Components

When I came to the section in the Vue course that introduced Vue Components as separate .vue files (e.g. App.vue), I noticed that the liferay-npm-bundler did not process the .vue files at all. 

We can see in Liferay's npm-vuejs-portlet template that the build script in package.json first processes the javascript with babel and then packages the code with the liferay-npm-bundler:

"scripts": { 
    "build": "babel --source-maps -d build/resources/main/META-INF/resources src/main/resources/META-INF/resources && liferay-npm-bundler" 
},

In order to make sure that all files of our Vue.js app are processed, including all the .vue components,  we'd have to run vue-cli-service first before being packaged by the liferay-npm-bundler.

So, the solution is to call "vue-cli-service build" instead of Babel. Therefore we first have to add the required dependencies to the project:

 

The build script then looks like this:

"scripts": { 
    "build": "vue-cli-service build --dest build/resources/main/META-INF/resources/lib --target lib --name index src/main/resources/META-INF/resources/**/*.js && liferay-npm-bundler" 
},

This processes all files in the META-INF/resources/lib folder with vue-cli-service and outputs it as one file "index.common.js" in the build folder, where the liferay-npm-bundler can then pick it up afterward. More about the vue-cli-service command(s) can be found here

The same command can actually be run from the command line as well:

./node_modules/.bin/vue-cli-service  build --dest build/resources/main/META-INF/resources/lib --formats commonjs --target lib --name index src/main/resources/META-INF/resources/**/*.js

The output looks like this:

We can see that vue-cli-service created 2 files in our build folder: index.common.js and index.css. The Javascript file will be picked up by the liferay-npm-bundler.  We'll talk about how  to handle the CSS file in the next section.

Scoped CSS

One of the neat things about using Vue Components is that you can add your CSS for that component into the same .vue file and make sure the scope is limited to that component.

 

00

More Blog Entries

7 Comments

ĐH
Đỗ Hưng 10 Months Ago
00
BT
Bep The gioi 9 Months Ago

https://thegioibepnhapkhau.vn/

00
BT
Bep The gioi 9 Months ago in reply to Bep The gioi .
00
ĐH
Đỗ Hưng 1 Month Ago

<a href="https://boschnhapkhau.vn/">Bosch nhập khẩu</a>

00
BN
Bosch nhập khẩu 1 Month Ago
00
BN
Bosch nhập khẩu 29 Days Ago

http://www.travelpeople.com.ph/Lists/Subscribers/DispForm.aspx?ID=7690

https://dongtrieu.quangninh.gov.vn/Lists/TinTucVote/AllItems.aspx?View={A57FA6AF-2DD0-4599-B332-B103B080AC72}&InitialTabId=Ribbon%2ERead&VisibilityContext=WSSTabPersistence&FilterField1=PageId&FilterValue1=https%3A%2F%2Fboschnhapkhau%2Evn%2F#InplviewHasha57fa6af-2dd0-4599-b332-b103b080ac72=Paged%3DTRUE-p_ID%3D360-PageFirstRow%3D361-FilterField1%3DPageId-FilterValue1%3Dhttps%253A%252F%252Fboschnhapkhau%252Evn%252F

https://www.napavalley.edu/it/Lists/Infopath%20Forms/AllItems.aspx#InplviewHash56e9f44d-a4bb-4a4a-b75c-b926ea8f1ea3=Paged%3DTRUE-p_ID%3D6077-PageFirstRow%3D6061

http://ju.edu.jo/Lists/Unsubscribe/DispForm.aspx?ID=12409

http://www.hio.gov.eg/Lists/DailyVisitors/DispForm.aspx?ID=14319

https://www.dmidcroms.com/CRS/Lists/DMIDCROMS%20Resources%20Feedback/DispForm.aspx?ID=2790

https://survey.sirim.my/surveyonline/Lists/F4F%20in%20Food%20Processing%20Survey/DispForm.aspx?ID=2218

https://haiha.quangninh.gov.vn/lists/gop%20y/allitems.aspx?View={7E514906-0FE6-4A87-BE79-4FD0DC0A7D5B}&FilterField1=TenNguoiGopY&FilterValue1=Bosch%20nh%E1%BA%ADp%20kh%E1%BA%A9u

http://nongson.gov.vn/Default.aspx?tabid=544&ctl=CTCH&mid=1572&CH=7098

https://info.undp.org/docs/dao/UNSP2015/Lists/PostSurvey/Item/displayifs.aspx?List=6e146e50-e299-46da-a20e-b9e885dace29&ID=10809

https://www.business.unsw.edu.au/forms-site/surveys/Lists/SMY%20Profile%20Information%20January%202016%20Intake/DispForm.aspx?ID=8497

http://www.sgae.es/Lists/Test_Form/Item/displayifs.aspx?List=e0b7fb61-7641-48cf-b099-c6ac54d06784&ID=1252

https://www.anh.gov.co/Inversionista/Lists/Atencin%20al%20Inversionista/AllItems.aspx#InplviewHash1455a241-d49b-4dcb-a9e7-0c568d9b2e10=FilterField1%3DLinkTitle-FilterValue1%3DBosch%2520nh%25E1%25BA%25ADp%2520kh%25E1%25BA%25A9u

https://partners.skanska.com/usa/projects/I15Exp/Pursuit/Public/SitePages/Outreach%20Administration%20Page.aspx#InplviewHashc865149a-6c32-48a6-9f6a-f92ce03caa59=FilterField1%3DFirstName-FilterValue1%3DBosch%2520

http://extranet.cstb.fr/sites/cost/Lists/Registration/AllItems.aspx?InitialTabId=Ribbon%2EListItem&VisibilityContext=WSSTabPersistence&View={DAA7FD7A-C797-4705-9F3D-F6AFA1187095}&FilterField1=FirstName&FilterValue1=Bosch%20nh%E1%BA%ADp%20kh%E1%BA%A9u

http://medicine.ju.edu.jo/Lists/ConfReg/DispForm.aspx?ID=28314

http://advance.captus.com/demo/Lists/Team%20Discussion/Flat.aspx?RootFolder=%2Fdemo%2FLists%2FTeam%20Discussion%2FBosch%20nh%E1%BA%ADp%20kh%E1%BA%A9u&FolderCTID=0x01200200F4DA0C6E7CC1B8488DEF1C2CA5A038EA

http://prodsurvey.portal1.com/Facial%20Polish%20and%20Serum/Lists/Product%20Trialing%20Survey/DispForm.aspx?ID=524

https://www.omangrid.com/en/Lists/HR_Training_Questioniers/DispForm.aspx?ID=909

https://portal.thomas-hardye.net/cz/sffd/Lists/Survey/DispForm.aspx?ID=11246

http://tlhmasterplan.mbakerintl.com/Lists/Feedback/AllItems.aspx?View={87456537-93CB-4DB0-82EE-EA5F0999CDFD}&FilterField1=Last%5fx0020%5fName&FilterValue1=https%3A%2F%2Fboschnhapkhau%2Evn%2F&InitialTabId=Ribbon%2ERead&VisibilityContext=WSSTabPersistence#InplviewHash87456537-93cb-4db0-82ee-ea5f0999cdfd=FilterField1%3DLast%255Fx0020%255FName-FilterValue1%3Dhttps%253A%252F%252Fboschnhapkhau%252Evn%252F-FilterField2%3DFirst%255Fx0020%255FName-FilterValue2%3DBosch%2520nh%25E1%25BA%25ADp%2520kh%25E1%25BA%25A9u

http://kw.pm.org/wiki/index.cgi?Boschnhapkhau365

https://pub.delta.edu/JobFair/_layouts/15/start.aspx#/Lists/Job%20Fair%20Registration%20Form/AllItems.aspx?View={ACAA8ECC-25A8-4728-9ACB-ECD6BF05690D}&FilterField1=LinkTitle&FilterValue1=Bosch%20nh%E1%BA%ADp%20kh%E1%BA%A9u#InplviewHashacaa8ecc-25a8-4728-9acb-ecd6bf05690d=FilterField1%3DLinkTitle-FilterValue1%3DBosch%2520nh%25E1%25BA%25ADp%2520kh%25E1%25BA%25A9u

https://www.just.edu.jo/FacultiesandDepartments/FacultyofEngineering/Departments/ChemicalEngineering/Lists/Alumni%20Survey/DispForm.aspx?ID=3517

https://ml007.k12.sd.us/PI/Lists/Post%20Tech%20Integration%20Survey/DispForm.aspx?ID=6803

http://www.mile.org.za/Lists/Knowledge%20Management/AllItems.aspx?FilterField1=LinkTitle&FilterValue1=Bosch%20nh%E1%BA%ADp%20kh%E1%BA%A9u&View={AAB4C6FE-75A3-441F-A2B4-2F339F8204AD}

http://frontlineapps.com.au/Lists/OldParadians/DispForm.aspx?ID=5986

http://admin.rostercoster.com/Lists/OldParadians/DispForm.aspx?ID=5986

https://www.kzntreasury.gov.za/Lists/FRAUD%20RISK%20ASSESSMENT%20QUESTIONNAIRE/DispForm.aspx?ID=19882

https://gnssn.iaea.org/main/SLS/Lists/2017%20Survey%20Pilot%20SLS/DispForm.aspx?ID=7411

https://www.gov.bn/Lists/eDarussalam%20Survey/DispForm.aspx?ID=70001

http://sharepoint.bath.k12.va.us/tech/forum/Lists/Forum%20Evaluation/DispForm.aspx?ID=5286

https://www.aerocivil.gov.co/atencion/participacion/encuesta/Lists/Rendicin%20de%20Cuentas%202017/DispForm.aspx?ID=21374

http://computer.ju.edu.jo/Lists/Alumni%20Feedback%20Survey/DispForm.aspx?ID=10146

http://sxd.quangnam.gov.vn/Default.aspx?tabid=158&TLID=14069

http://vpubnddonggiang.gov.vn/Default.aspx?tabid=65&TLID=17997

http://bttnmuongnhe.org.vn/portal/Lists/Faq/DispForm.aspx?ID=1130

http://rsc.gov.vn/SitePages/faqDetail.aspx?item=5382

00
BN
Bosch nhập khẩu 29 Days Ago

http://www.travelpeople.com.ph/Lists/Subscribers/DispForm.aspx?ID=7690

 

https://dongtrieu.quangninh.gov.vn/Lists/TinTucVote/AllItems.aspx?View={A57FA6AF-2DD0-4599-B332-B103B080AC72}&InitialTabId=Ribbon%2ERead&VisibilityContext=WSSTabPersistence&FilterField1=PageId&FilterValue1=https%3A%2F%2Fboschnhapkhau%2Evn%2F#InplviewHasha57fa6af-2dd0-4599-b332-b103b080ac72=Paged%3DTRUE-p_ID%3D360-PageFirstRow%3D361-FilterField1%3DPageId-FilterValue1%3Dhttps%253A%252F%252Fboschnhapkhau%252Evn%252F

 

https://www.napavalley.edu/it/Lists/Infopath%20Forms/AllItems.aspx#InplviewHash56e9f44d-a4bb-4a4a-b75c-b926ea8f1ea3=Paged%3DTRUE-p_ID%3D6077-PageFirstRow%3D6061

 

http://ju.edu.jo/Lists/Unsubscribe/DispForm.aspx?ID=12409

 

http://www.hio.gov.eg/Lists/DailyVisitors/DispForm.aspx?ID=14319

 

https://www.dmidcroms.com/CRS/Lists/DMIDCROMS%20Resources%20Feedback/DispForm.aspx?ID=2790

 

https://survey.sirim.my/surveyonline/Lists/F4F%20in%20Food%20Processing%20Survey/DispForm.aspx?ID=2218

 

https://haiha.quangninh.gov.vn/lists/gop%20y/allitems.aspx?View={7E514906-0FE6-4A87-BE79-4FD0DC0A7D5B}&FilterField1=TenNguoiGopY&FilterValue1=Bosch%20nh%E1%BA%ADp%20kh%E1%BA%A9u

http://nongson.gov.vn/Default.aspx?tabid=544&ctl=CTCH&mid=1572&CH=7098

 

https://info.undp.org/docs/dao/UNSP2015/Lists/PostSurvey/Item/displayifs.aspx?List=6e146e50-e299-46da-a20e-b9e885dace29&ID=10809

 

https://www.business.unsw.edu.au/forms-site/surveys/Lists/SMY%20Profile%20Information%20January%202016%20Intake/DispForm.aspx?ID=8497

 

http://www.sgae.es/Lists/Test_Form/Item/displayifs.aspx?List=e0b7fb61-7641-48cf-b099-c6ac54d06784&ID=1252

 

https://www.anh.gov.co/Inversionista/Lists/Atencin%20al%20Inversionista/AllItems.aspx#InplviewHash1455a241-d49b-4dcb-a9e7-0c568d9b2e10=FilterField1%3DLinkTitle-FilterValue1%3DBosch%2520nh%25E1%25BA%25ADp%2520kh%25E1%25BA%25A9u

 

https://partners.skanska.com/usa/projects/I15Exp/Pursuit/Public/SitePages/Outreach%20Administration%20Page.aspx#InplviewHashc865149a-6c32-48a6-9f6a-f92ce03caa59=FilterField1%3DFirstName-FilterValue1%3DBosch%2520

 

http://extranet.cstb.fr/sites/cost/Lists/Registration/AllItems.aspx?InitialTabId=Ribbon%2EListItem&VisibilityContext=WSSTabPersistence&View={DAA7FD7A-C797-4705-9F3D-F6AFA1187095}&FilterField1=FirstName&FilterValue1=Bosch%20nh%E1%BA%ADp%20kh%E1%BA%A9u

 

http://medicine.ju.edu.jo/Lists/ConfReg/DispForm.aspx?ID=28314

 

http://advance.captus.com/demo/Lists/Team%20Discussion/Flat.aspx?RootFolder=%2Fdemo%2FLists%2FTeam%20Discussion%2FBosch%20nh%E1%BA%ADp%20kh%E1%BA%A9u&FolderCTID=0x01200200F4DA0C6E7CC1B8488DEF1C2CA5A038EA

 

http://prodsurvey.portal1.com/Facial%20Polish%20and%20Serum/Lists/Product%20Trialing%20Survey/DispForm.aspx?ID=524

https://www.omangrid.com/en/Lists/HR_Training_Questioniers/DispForm.aspx?ID=909

 

https://portal.thomas-hardye.net/cz/sffd/Lists/Survey/DispForm.aspx?ID=11246

 

http://tlhmasterplan.mbakerintl.com/Lists/Feedback/AllItems.aspx?View={87456537-93CB-4DB0-82EE-EA5F0999CDFD}&FilterField1=Last%5fx0020%5fName&FilterValue1=https%3A%2F%2Fboschnhapkhau%2Evn%2F&InitialTabId=Ribbon%2ERead&VisibilityContext=WSSTabPersistence#InplviewHash87456537-93cb-4db0-82ee-ea5f0999cdfd=FilterField1%3DLast%255Fx0020%255FName-FilterValue1%3Dhttps%253A%252F%252Fboschnhapkhau%252Evn%252F-FilterField2%3DFirst%255Fx0020%255FName-FilterValue2%3DBosch%2520nh%25E1%25BA%25ADp%2520kh%25E1%25BA%25A9u

 

http://kw.pm.org/wiki/index.cgi?Boschnhapkhau365

 

https://pub.delta.edu/JobFair/_layouts/15/start.aspx#/Lists/Job%20Fair%20Registration%20Form/AllItems.aspx?View={ACAA8ECC-25A8-4728-9ACB-ECD6BF05690D}&FilterField1=LinkTitle&FilterValue1=Bosch%20nh%E1%BA%ADp%20kh%E1%BA%A9u#InplviewHashacaa8ecc-25a8-4728-9acb-ecd6bf05690d=FilterField1%3DLinkTitle-FilterValue1%3DBosch%2520nh%25E1%25BA%25ADp%2520kh%25E1%25BA%25A9u

 

https://www.just.edu.jo/FacultiesandDepartments/FacultyofEngineering/Departments/ChemicalEngineering/Lists/Alumni%20Survey/DispForm.aspx?ID=3517

 

https://ml007.k12.sd.us/PI/Lists/Post%20Tech%20Integration%20Survey/DispForm.aspx?ID=6803

 

http://www.mile.org.za/Lists/Knowledge%20Management/AllItems.aspx?FilterField1=LinkTitle&FilterValue1=Bosch%20nh%E1%BA%ADp%20kh%E1%BA%A9u&View={AAB4C6FE-75A3-441F-A2B4-2F339F8204AD}

 

http://frontlineapps.com.au/Lists/OldParadians/DispForm.aspx?ID=5986

 

http://admin.rostercoster.com/Lists/OldParadians/DispForm.aspx?ID=5986

 

https://www.kzntreasury.gov.za/Lists/FRAUD%20RISK%20ASSESSMENT%20QUESTIONNAIRE/DispForm.aspx?ID=19882

 

https://gnssn.iaea.org/main/SLS/Lists/2017%20Survey%20Pilot%20SLS/DispForm.aspx?ID=7411

 

https://www.gov.bn/Lists/eDarussalam%20Survey/DispForm.aspx?ID=70001

00