I think you have all the elements you need to switch your attitude towards games, so first off, well done on having the awareness to identify the issue and to begin asking how to go about remedying it.
Let's talk about competitive games first and the attitude towards winning and losing
Recognise that you're not going to win every game. You may even lose some games that looked like a sure bet, and you may win some games that looked like a certain loss. This is the inherent fun in many competitive games - the outcome is uncertain. If you stop trying and self sabotage, the outcome will always be certain, i.e. it will always be a loss.
If you recognise this, then every game is worth playing through to the end, and if your opponent is also decent, as most players are, they will recognise your efforts for trying your hardest.
You can also put the shoe on the other foot. Imagine that you're playing a really close game and after battling hard, things have swung in your favour. You want to keep fighting through to the end to secure the victory. Imagine now that the opponent at this moment throws in the towel, and torpedoes the game. This hollows out your victory. Don't be that player. Try to empathise with your opponent as someone who equally deserves a chance to enjoy the game and compete for a worthy victory.
Finally, learning how to turn a loss into a win is a skill in and of itself. Executing a comeback is sometimes more satisfying than a complete stomp, and more entertaining for all players of the game. You won't practice that if you check out as soon as the tide is against you, and you're depriving yourself of some more fun games.
Now let's look at an attitude to improving your overall skill
Some people take the attitude of looking at their long term skill as a player, and this may include their rank, win/loss ratio or season stats. Since they take a macro view, one loss may not detract from their long term improvement and helps put their temporary upset at losing into perspective. Even if you lose games on average, you can set yourself a target to strive for. The losses will still suck, but if you self sabotage then you also scuttle your long term goals.
Part of this may be an approach of wanting to improve focussed skills within the game that don't rely on win or loss, per se, but will help build your overall proficiency. This may be perfecting your early game setup and speed, or your ability to execute on a particular strategy, or to lay traps for your opponent. Since I don't know what games you play I can't suggest what that would look like, I've just used general terms. You need iterations to work out what's effective, and a loss is useful information that a strategy, when followed through, may not be effective. If you don't learn from that then you may repeat the same mistakes, leading to more losses in the future.
You may also take this as an opportunity to recognise shared mechanics across games, so that playing a game is a chance to improve at others, too. Does this game have a similar economy to other games you've played, or hand management, or push your luck elements.
Now let's look at an attitude of enjoying the game for its own sake
If you and your opponent are giving it your all, then sometimes the whole aspect of win/loss can dissolve away, and be replaced with sheer enjoyment of trying your hardest regardless of the outcome. Trying to wrack your brain when you're behind and wring out every possible mechanic or avenue for victory can be just as exhilarating as trying to hold onto a seemingly unassailable win. Trying to find that flow state where you don't care about the outcome is worthwhile, I hope you can seek it out.
Finally, don't mourn your defeat, instead, celebrate your opponent's victory
You tried every trick in the book, and they still won. Wow, that's impressive! After the initial sting of loss, congratulate your opponent and turn their victory into your own. Compliment their strategy! Ask them how they managed to pull off that trick on the third turn. Ask if they have any tips, or how they spotted the gaps in your plans. If they're a gracious winner, they will be happy to talk shop when the round is over. If you share in their win, you make up in some way for your loss, rather than doubling down on those negative feelings.
This goes especially for boardgames or games where you know the other players and opponents. If they're your friends or family, then you should be happy that they're happy and they'll in kind work to assuage your upset in defeat rather than letting you sulk.
I realised this because I play a lot of boardgames with a good friend, and as soon as the scores are tallied, the first thing we always do is congratulate one another for a good game and immediately turn to talking about how one another played and what we're going to try next game. It turns even a competitive game into a team effort where we're working together to eke as much enjoyment out of the game as possible, and push one another to try harder next round.
So yeah, share the joy don't dwell on the woe, and you'll turn every win into something to be celebrated together.
I won't talk to playing cooperative games, as other posters have addressed that. Putting yourself as a me/them into an 'us' game can divert your pastime to something more productive, but it may not break down your 'sore loser' problem enough that it wouldn't be an issue when you return to competitive games.
If you want a good video, I like Otzdarva's explanation of how not to get 'tilted' in online games.