unable to use vue-cookies outside vue component

I am working with Vue. Trying to create cookies using the vue-cookies library.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[[vue-cookies](https://www.npmjs.com/package/vue-cookies)]
</code>
<code>[[vue-cookies](https://www.npmjs.com/package/vue-cookies)] </code>
[[vue-cookies](https://www.npmjs.com/package/vue-cookies)]

for simplicity lets say I have 2 files a component file. register.vue,
and an authentication typescript file. auth.ts

I am trying to setup the cookies in the auth.ts such that I can then import elsewhere. What should be a trivial task is proving challenging. I do not know how to setup the cookies,and the method recommended in the docs are brief and somewhat unclear.
The data is a jwt, and it is created server side. After going to the register route on the server the created jwt is passed via http response back to the client side vue application. The intention is to store it via cookies, and pass it as necessary back to the server on any needed routes. auth.ts will only run client side (browser). Setting and reading the token is being done inorder to store the passed in jwt, and pass the info back to the server to protected routes as needed.

// file name : auth.ts

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import VueCookies from 'vue-cookies'
</code>
<code>import VueCookies from 'vue-cookies' </code>
import VueCookies from 'vue-cookies'    

//i tried what the npm docs seems to recommend. It doesnt work.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const $cookies = inject<VueCookies>('$cookies')
</code>
<code>const $cookies = inject<VueCookies>('$cookies') </code>
const $cookies = inject<VueCookies>('$cookies') 

// i tried the below. It is Not callable and doesnt work

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const cookies = VueCookies()
</code>
<code>const cookies = VueCookies() </code>
const cookies = VueCookies() 

// i tried this and it doesnt work.
const cookies = VueCookies('$cookies')

Once I have the cookies object instatiated properly, the intention is to proceed like so

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> cookies.config('2d')
cookies.set('abc', '123) etc.
</code>
<code> cookies.config('2d') cookies.set('abc', '123) etc. </code>
   cookies.config('2d')
    cookies.set('abc', '123) etc.

I tried many more options, which dont work. Reading the vue-cookies code base has so far proven a bit tricky.

There arent a lot of examples to go off of, and I am still learning Vue and typescript. Any assistance would be appreciated. Again I am using vue 3 with composition API and trying to set a cookie in a typescript file using the vue-cookies library.

Related post. Here the OP had the same issue. However the response provided doesnt quite answer the question directly though (though the responder alludes to why ‘inject’ might not work outside components hierarchy, but doesn’t seem to propose an alternative…)
[/questions/75551682/vue3-how-to-work-with-cookies-outside-components]

New contributor

sope lanlehin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật